• Resolved Johann ROCHE

    (@johannroche)


    Hi,

    I have created a function that checks if the user is over 18 years old.
    If not, registration is not allowed.

    I implemented this using the custom validation method you provided in the documentation: https://www.cozmoslabs.com/docs/profile-builder-2/developers-knowledge-base/custom-fields/add-extra-validation-to-a-custom-field/

    It woks but I encountered an issue. : This validation is applied to all birthdate fields on my website instead of just the registration form.

    Is there a way to apply this validation specifically to the registration form?

    /*
    
    * Custom Birth Date Validation
    
    */
    
    /* handle field validation */
    
    add_filter('wppb_check_form_field_datepicker', 'wppbc_custom_birthdate_validation', 20, 4);
    
    function wppbc_custom_birthdate_validation($message, $field, $request_data, $form_location)
    
    {
    
    if ($field['field'] == 'Datepicker' && $field['meta-name'] == 'birthdate') {
    
    if (isset($request_data[$field['meta-name']]) && trim($request_data[$field['meta-name']]) != '') {
    
    $birthdate = DateTime::createFromFormat('d/m/Y', $request_data[$field['meta-name']]);
    
    if ($birthdate === false) {
    
    return 'Invalid birth date format.';
    
    }
    
    $now = new DateTime();
    
    $interval = $now->diff($birthdate);
    
    $age = $interval->y;
    
    if ($age < 18) {
    
    $locale = get_locale();
    
    $lang = substr($locale, 0, 2);
    
    $ageOfKid = "As you are under 18, one of your parents must create an account and then add you as a child from his account.";
    
    if ($lang == "de") {
    
    $ageOfKid = "Da du unter 18 Jahre alt bist, muss einer deiner Eltern ein Konto erstellen und dich dann als Kind von ihrem Konto aus hinzufügen.";
    
    } elseif ($lang == "fr") {
    
    $ageOfKid = "étant donné que vous avez moins de 18 ans, l'un de vos parents doit créer un compte puis vous ajouter en tant qu'enfant à partir de son compte.";
    
    }
    
    return $ageOfKid;
    
    }
    
    }
    
    if ((isset($request_data[$field['meta-name']]) && (trim($request_data[$field['meta-name']]) == '')) && ($field['required'] == 'Yes')) {
    
    return wppb_required_field_error($field["field-title"]);
    
    }
    
    }
    
    return $message;
    
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Custom Validation Only on Register Form ?’ is closed to new replies.