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.

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
#1340569

I have tried to create the same code for another form of the same web page, but when activating the new custom code, the page runs out of service.
Does the code have any errors?

<?php
/**
* New custom code snippet.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_filter( 'cred_form_validate', 'require_featured_image_validation', 10, 2 );
function require_featured_image_validation( $data, $form_data ) {
$forms = array( 37147 );
if( in_array( $form_data['id'], $forms ) ){
list($fields,$errors)=$data;
if ($fields['wpcf-contrasena-sugerencia']['value']!=$fields['wpcf-repita-la-sugerencia']['value'])
{
//set error message for pass2
$errors['repita-la-sugerencia']='Las contraseñas no coinciden';
}
$data =array($fields,$errors);
}
return $data;
}