I am trying to: On User Registration Form (CRED Create User Form), make First Name and Last Name required with front end validation on submit, similar to Username, Email fields.
Dear darry,
In the front-end, you can add attribute "required = 'required'" in to both the first_name field and the last_name field, for example, edit your CRED form, in the JS editor window, add below codes:
jQuery( document ).ready(function(){
jQuery( "input[name='first_name']").attr('required', 'required');
jQuery( "input[name='last_name']").attr('required', 'required');
});
In the server side, please follow our document to setup a PHP funtion to validate those field:
https://toolset.com/documentation/user-guides/cred-api/#cfv
Validation Hooks
cred_form_validate: (filter)
This hook provides custom validation for form fields.
Is there a way to generate the same label element with class="wpt-form-error" as the Username field and Email field, if I click "Submit" with the First Name field and Last Name field blank? See screen shot.
Yes, it is possible with CRED filter hook "cred_filter_field_before_add_to_form", for example you can add below codes into your theme/functions.php:
add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 2);
function required_fields_func($field, $computed_values){
if(in_array($field['id'], array('first_name', 'last_name'))){
$field['data']['validate']['required'] = array (
'active' => 1,
'value' => 1,
'message' => 'This field is required'
) ;
}
return $field;
}
Thank you so much! That is exactly the solution I was looking for.
In php 7.1 it is giving error.