Tell us what you are trying to do? I am trying to create a field call "Repeat password" in which they must enter the same password in a cred form and it must be the same. Example;
Choose a password (minimum 7 digits)
Repeat password
It must validate password and give an error value if they are not equal
How to show an error if the passwords do not match when filling out the form?
Is there any documentation that you are following? no
Is there a similar example that we can see?
What is the link to your site? hidden link
Hi, if you're talking about the password in a User registration form, this functionality is built-in to the system. So I assume you're talking about some other password field. In this case, the only way to validate and compare the two fields is to use the Forms API and some custom code. Here's the documentation for this API:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Here's a basic example you can add to a child theme's functions.php file, or to a new snippet in Toolset > Settings > Custom Code:
add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
$forms = array( 1234 );
if( in_array( $form_data['id'], $forms ) ){
list($fields,$errors)=$data;
if ($fields['wpcf-pass1']['value']!=$fields['wpcf-pass2']['value'])
{
//set error message for pass2
$errors['pass2']='Passwords do not match';
}
$data =array($fields,$errors);
}
return $data;
}
Change 1234 to match your Form ID, and change pass1 and pass2 to match your password field slugs. Let me know if you have questions about this.
Thanks, David.
Would it work in my case if I write the following?
add_filter( 'cred_form_validate', 'error_fields', 10, 2 );
function require_error_fields_validation( $data, $form_data ) {
$forms = array( 1234 );
if( in_array( $form_data['(ID: 36925)'], $forms ) ){
list($fields,$errors)=$data;
if ($fields['wpcf-pass1']['value']!=$fields['wpcf-pass2']['value'])
{
//set error message for pass2
$errors['pass2']='Passwords do not match';
}
$data =array($fields,$errors);
}
return $data;
}
I would have to add something else like cred_form_validate and where should I add it?
unfortunately i don't have much idea of php
Not quite, check the screenshot here for a visual reference of some issues. The only things you should change from my original code are 1234, pass1, and pass2.
1. See the red highlights. The function name must match the cred_form_validate hook, so change error_fields back to require_error_fields_validation as I had it written originally.
2. See the blue highlights. If you want to validate Form 36925, place that number in the forms array instead of 1234. Change the "if" line back to how it was originally written.
3. See the green highlights. I'm not sure of your custom field slugs. If they are "pass1" and "pass2", then I made a very lucky guess 🙂 If they are not "pass1" and "pass2", you should change these slugs to match your actual custom fields.
Thanks Christian,
I have made the changes, according to your instructions. I will include it in Toolset> Settings> Custom Code.
add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
$forms = array(36925);
if( in_array( $form_data['id'], $forms ) ){
list($fields,$errors)=$data;
if ($fields['wpcf-contrasena-denuncia']['value']!=$fields['wpcf-repita-la-denuncia']['value'])
{
//set error message for pass2
$errors['repita-la-denuncia']='Passwords do not match';
}
$data =array($fields,$errors);
}
Reports and complaints form
Los datos recibidos en nuestro canal de reclamos son los siguientes:
return $data;
}
It is necessary to do something else.
Reports and complaints form
Los datos recibidos en nuestro canal de reclamos son los siguientes:
You should not add plain text like this, it is invalid in PHP. Please remove these two lines and test again. If it's still not working, I'll need to log in and take a closer look. Please provide login credentials in the private reply fields here.
Thank you. I have removed these two lines and now the form works, but if you enter different passwords nothing happens.
In your screenshot, I can see it says this code snippet is "Inactive." This should be "Active", so hover over the snippet and click "Activate". If you already made that change but it's still not working, please provide admin login credentials here so I can take a closer look.
hello Christian,
I've got it activated and it works perfectly. All that remains is for the text 'Passwords do not match' to appear. I can give you access and I need you to tell me which country you come from so that my hosting gives you access. Do you need ftp access? or keys to wp?
Okay great, please provide WP login credentials in the private reply fields here. I do not think I need FTP, just an admin-level WordPress login. I am in the USA, please let me know if you need anything more specific to give me access.
Hi, I was able to log in using these credentials but I'm not able to access wp-admin. I can see the admin bar at the top of the screen, but admin URLs redirect to the homepage. Is this the security measure you mentioned?
I think you already have authorization. Can you try again, please. Thank you
It looks like the Form messages field has been deleted. This field is required for showing validation and error messages. See the screenshot attached here - you must add the Form messages field somewhere in the Form. Otherwise, validation messages may not appear as expected. Here's the field shortcode format:
[cred_field field='form_messages' class='alert alert-warning']
Please add this to the Form contents and let me know if the validation message still does not appear.
Perfect Christian. everything works correctly.thanks
Excellent! Feel free to open a new ticket if you notice any other issues.