Skip Navigation

[Resolved] Cred user field not required

This thread is resolved. Here is a description of the problem and solution.

Problem:
Validation and Required field option should work at once in CRED User form for native WP Fields (e.g. Last Name), same as builtin CRED form style.

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

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('last_name', 'adresse'))){
        $field['data']['validate']['required'] = array ( 
            'active' => 1, 
            'value' => 1, 
            'message' => 'This field is required.'
        ) ;
    }
    return $field;
}

==> Make sure to replace 'last_name', 'adresse' with your field slugs on line #3
==> You can modify the message text as needed in above code on line #7.

2. OR If you want to have JS validation, you can use this (modification will be needed):
https://toolset.com/forums/topic/cred-user-field-not-required/#post-567100

This support ticket is created 7 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Pat 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#564356

Pat

Hello,

I'm building a Cred user form and have standard user fields (like last_name) and specific user fields created with Types.
Now, I want all fields to be required in the form. For all new created fields, no issue, but with the standard "last_name" field, it is not considered as required in the form.

Here is the code I'm using :
[cred_field field='last_name' post='user' value='' urlparam='' class='form-control' output='bootstrap' required='true']

Let me know if there is something to do on this.
Regards
Pat

#564380

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Pat,

Thank you for getting in touch with us. Yes, you can make any field required using this jQuery code. Please add this in your User CRED form >> JS editor section:

jQuery( document ).ready(function() {
    jQuery("input[name='last_name']").prop('required',true);
});

You can make more fields required by copying this line and replacing field slug in the above code.

I hope it helps, Thank you

#564396

Pat

Hi Noman,

Thanks for the info. This is working, but the issue is that the validation is not done in the same time that the other ones. The last_name validation came first and then, if you fill the field, then you have the other validations that are display in error if the fields are empty.

Is there a way to add a validation for last_name that is managed the same way than for the other fields?
Regards
Pat

#564432

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

The builtin style that you see is coming from CRED and we don’t have a way to add that to the native WP user fields. Could be possible using some in-depth custom code, old ticket:
https://toolset.com/forums/topic/mark-first_name-and-last_name-as-required/#post-432423

1. I would suggest to add this style to all the fields you want to make required, this way it will show consistent required message and in correct order too:

jQuery( document ).ready(function() {
    jQuery("input[name='user_login']").prop('required',true);
    jQuery("input[type='password']").prop('required',true);
    jQuery("input[name='last_name']").prop('required',true);
});

-- And add text before or after each form field in the CRED like “Required”, so that user knows which fields are required.

2. Alternate way would be using this method, but this will run after form is being submitted:
https://toolset.com/forums/topic/logical-problem/

Thank you

#566625

Pat

Hi Noman,

I have tried to place all needed validations with your jquery solution, but this is not working. Here is what I placed in the jquery tab :
jQuery( document ).ready(function() {

jQuery("input[name='last_name']").prop('required',true);
jQuery("input[name='adresse']").prop('required',true);
jQuery("input[name='user_email']").prop('required',true);
});

If I try to validate an empty form, only the first field (last_name) is taken into consideration.
Regards
Pat

#566662

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Pat,

Okay, I need to look into your site, please provide temporary access (WP-Admin and FTP Login info) to your staging site.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Links to the [CRED Form] Edit Screen and [Page] where you have inserted this Form.

Thankyou

#567100

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Pat,

I have updated the code and now it is working as expected, please check here:
hidden link

jQuery( document ).ready(function() {
	jQuery("input[name='wpcf-types-de-pros']").prop('required',true);
    jQuery("input[name='last_name']").prop('required',true);
    jQuery("input[name='wpcf-nom-et-prenom-du-responsable']").prop('required',true);
	jQuery("input[name='user_email']").prop('required',true);
    jQuery("input[name='wpcf-adresse']").prop('required',true);
    jQuery("input[name='wpcf-siret']").prop('required',true);
	jQuery("input[name='wpcf-telephone']").prop('required',true);
});

Thank you

#567110

Pat

Hi Noman,
Thanks for the feedback.
I just tested it and I'm not sure of the result :
The validation now works field by field, which means, you have to click onthe submit button to see the first notification, then, fill the required field and press again on the submit button to have the second notification which is displayed.

I would prefer to have the same way of working than the standard Cred validation (ie : the first time you press on the submit button, you have all needed required fields that are highlighted).

Any idea?
Regards
Pat

#567221

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I am able to find another workaround. I have removed js validation and added it using php code and cred filter hook, now it works same way as builtin cred style:
hidden link

I have added this code in theme’s functions.php file, you can modify the message text as needed:

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('last_name', 'adresse'))){
        $field['data']['validate']['required'] = array ( 
            'active' => 1, 
            'value' => 1, 
            'message' => 'Ce champ est requis.'
        ) ;
    }
    return $field;
}

I believe its good now. Thanks

#567337

Pat

Hi Noman,
Sounds good.
Thanks for your support
Regards
Pat