Skip Navigation

[Resolved] How to show an error if the passwords do not match?

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

Problem: I have a Form that captures two passwords. I would like to display an error message if the two passwords do not match.

Solution: Use the Forms API cred_form_validate to compare the two field values.

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);
}
return $data;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 15 replies, has 2 voices.

Last updated by pedroS-6 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1332501

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

#1332873

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.

#1334561

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

#1334587
Screen Shot 2019-09-08 at 4.37.50 PM.png

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.

#1334751

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.

#1335237


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.

#1335607
0.png
00.png

Thank you. I have removed these two lines and now the form works, but if you enter different passwords nothing happens.

#1335879

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.

#1336227
0.png
00.png

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?

#1336843

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.

#1338071

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?

#1338415

I think you already have authorization. Can you try again, please. Thank you

#1338937
Screen Shot 2019-09-12 at 11.27.17 AM.png

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.

#1338961

Perfect Christian. everything works correctly.thanks

#1339215

Excellent! Feel free to open a new ticket if you notice any other issues.