Home › Toolset Professional Support › [Resolved] CRED Validation not working (requiring First and Last Name)
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Tagged: CRED API, Toolset Forms, User-registration forms
Related documentation:
This topic contains 18 replies, has 2 voices.
Last updated by TomT2847 6 years, 8 months ago.
Assisted by: Nigel.
I am trying to: Use the cred_form_validate hook to validate that the first and last name fields are filled out of a form that creates a new user.
Link to a page where the issue can be seen: hidden link
I expected to see: An error message when the form is submitted and the first or last name fields are empty.
Instead, I got: No error messages.
Here is the code that I've included in functions.php, which is borrowed heavily from https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
add_filter('cred_form_validate','my_validation',10,2); function my_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //validate if specific form if ($form_data['id']==1746) { //check first_name field if ( empty($fields['wpcf-first_name']['value']) ) { //set error message for first_name field $errors['wpcf-first_name']='This field is required'; } if ( empty($fields['wpcf-last_name']['value']) ) { //set error message for first_name field $errors['wpcf-last_name']='This field is required'; } } return array($fields,$errors); }
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Tom
At first glance that code looks valid, I would have to do some debugging to understand why it's not working.
But before that, if you simply want the fields to be required, why not set them as required in the field options, then your custom validation becomes redundant.
Go to Toolset > User Fields and edit the User Field Group in question.
Then expand the field, e.g. First Name, and set is as required.
This will then be applied in your CRED form.
Hi Nigel,
Thanks for getting back to me. Unfortunately I don't see the First Name and Last Name fields in the User Fields section. I don't actually remember ever creating them either. They seem to be hooked into the default WordPress First Name and Last Name fields, so something is going on behind the scenes automatically which seems to suggest that they weren't generated by me in the User Fields section.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Tom
Yes, of course, you are right, the first name and last name are standard user fields.
You threw me because in your sample code the fields you are checking for are 'wpcf-first_name' and 'wpcf-last_name'.
With Types custom fields they are stored in the database with the prefix 'wpcf-'.
The solution in your case is to simply omit the prefix, because these are in fact standard fields and so do not begin with 'wpcf-'.
So replace them with just 'first_name' and 'last_name' and you should find your code working as expected.
Hi Nigel,
Oh, that makes sense! Thanks for catching that. Weirdly, the changes still aren't working. I've cleared my server cache, CDN cache, and browser cache as well, so it's not that. Any other suggestions?
add_filter('cred_form_validate','my_validation',10,2); function my_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //validate if Create Account form if ($form_data['id']==1746) { //check first_name field if ( empty($fields['first_name']['value']) ) { //set error message for first_name field $errors['first_name']='This field is required'; } if ( empty($fields['last_name']['value']) ) { //set error message for last_name field $errors['last_name']='This field is required'; } } return array($fields,$errors); }
Edit: fixed a typo.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
That's odd, because I have the same code on my test site and it is working fine.
The only difference, of course, is the ID of the cred form. Are you sure yours is correct?
It's not something where I would expect a conflict to arise, but could you try again on a minimal install, meaning deactivating other plugins and switching to the twentyseventeen theme (bear in mind you'll need to copy across the code to the new theme's functions.php file if that's how you are adding it).
If you still have the problem double check your debug logs and the browser console for errors.
If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false);
That will create a debug.log file in your wp-content directory which you can examine in any text editor. Try visiting the same page with the CRED form again and then inspect the log.
It wouldn't hurt to throw in a couple of debug messages into your code, e.g.
/** * Validate User first and last name */ function tssupp_validate_names( $error_fields, $form_data ){ error_log("Validating form..."); list($fields,$errors) = $error_fields; if ( $form_data['id'] == 1746 ) { error_log("Checking fields..."); error_log(print_r($fields, true)); //check first_name field if ( empty($fields['first_name']['value']) ) { //set error message for first_name field $errors['first_name']='This field is required'; } if ( empty($fields['last_name']['value']) ) { //set error message for first_name field $errors['last_name']='This field is required'; } } return array( $fields, $errors ); } add_filter( 'cred_form_validate', 'tssupp_validate_names', 10, 2 );
Let me know if you find anything meaningful or unexpected.
Hi Nigel,
Thanks for the suggestions. I tested the site on a staging site with all other plugins turned off and with the twentyseventeen theme. Still didn't work.
I then turned on debug logging and tested the function. No errors were logged to the console. I then added an error log outside of the function, but inside functions.php, and an error was logged to the console. This says to me that the function "tssupp_validate_names" isn't even being called, which means it's not getting hooked into cred_form_validate.
Here's the staging site: hidden link
Here's the code:
error_log("Errors are logging"); // This is being logged /** * Validate User first and last name */ function tssupp_validate_names( $error_fields, $form_data ){ error_log("Validating form..."); // This isn't being logged list($fields,$errors) = $error_fields; if ( $form_data['id'] == 1746 ) { error_log("Checking fields..."); error_log(print_r($fields, true)); //check first_name field if ( empty($fields['first_name']['value']) ) { //set error message for first_name field $errors['first_name']='This field is required'; } if ( empty($fields['last_name']['value']) ) { //set error message for last_name field $errors['last_name']='This field is required'; } } return array( $fields, $errors ); } add_filter( 'cred_form_validate', 'tssupp_validate_names', 10, 2 );
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Tom
I'm at something of a loss to explain that.
Before we do anything else, can you try disabling all non-Toolset plugins and try again to see whether the "Validating form" message gets sent to the debug.log file. If not, try switching theme to twentyseventeen.
If you still don't see it I'll probably need to take a copy of your site for testing, but let me know the results of the above first.
Hi Nigel,
Disabled all plugins except the following four: Toolset Access, Toolset CRED, Toolset Types, Toolset Views. Theme is twentyseventeen.
This is a staging site with the problem: hidden link
No errors are being logged except for the first one outside of the cred_form_validate hook (see code in my last post). Is that the right hook name?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
That's the right hook, yes.
I think I'll need a copy of your site to do some testing locally.
Could you provide a duplicate, as described here: https://toolset.com/faq/provide-supporters-copy-site/
If you have problems with Duplicator, try All in One WP Migration, or if you are struggling let me know and I'll go in and take a copy myself.
I'll mark your next reply as private.
Unfortunately, I cannot give you access to the site or create a copy of the site. That would violate our Privacy Policy. Any other options?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Tom
There isn't much I can do without being able to inspect and test your site, as the problem seems to be peculiar to your site, I cannot reproduce it.
Going back to your previous comment, can you please add just the following:
error_log("Inside functions.php"); function tssupp_test_form_validate( $fields ){ error_log("Inside hooked function"); return $fields; } add_filter( 'cred_form_validate', 'tssupp_test_form_validate' );
Then try and submit a CRED form (any form).
In your debug.log you should see the "inside functions.php" message, and also the "inside hooked function" message.
If you don't see the second message that's a fundamental stumbling block.
In that case all I can suggest is you uninstall CRED, download a fresh copy and re-install it, and try making a new CRED form for testing.
Otherwise, without access, I'm pretty much at the end of the road.
Let me know if the above throws anything up.
Hi Nigel,
Tested code you sent, only "Inside functions.php" is logging, which means the hook isn't working. Deleted and reinstalled the CRED plugin, same problem. This hook is working on other websites?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Tom
We use the hook routinely, and I just tested the same code on a User Form and it worked as expected.
I've never come across this before. There is no way for us to debug it without a copy of the site.
You could, as an alternative, just add a snippet of JS to add the required attribute to the input fields, which will use the browser's own validation to require that the fields are completed.
Add this to the custom JS section of your form.
( function( $ ) { $( document ).ready( function(){ $('input[name="first_name"]').prop("required", true); $('input[name="last_name"]').prop("required", true); }); })( jQuery );
Hi Nigel, sorry for the delay. I want a solution that will work if JS is disabled, so that won't really help. I need to do other validation with the data submitted by users as well, so I need to get this working. I think I can give you access to a staging site, if you can provide me with an option to send the details in a private reply.