Skip Navigation

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

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

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

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 5 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1342015

I am trying to: I am trying to to validate and compare two fields using the Forms API and some custom code. Christian Cox helped me and use de following code

<?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( 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']='La contraseña no es correcta';
}
$data =array($fields,$errors);
}
return $data;
}
It works perfectly. worse when creating a new custom code and do the same with another form by changing the form number and the two fields, the web page is blocked.
Where is the problem?

<?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;
}

Link to a page where the issue can be seen: hidden link

I expected to see:

Instead, I got: error, and the page out of order

#1342057

Hi Pedro,

Thank you for contacting us and I'd be happy to assist.

The issue seems to be that the same function name "require_featured_image_validation" cannot be repeated for the second code snippet too, as function names need to be unique.

To fix this, you can include the target form's ID in the name of the functions, so that the first code snippet becomes:


add_filter( 'cred_form_validate', 'require_featured_image_validation_36925', 10, 2 );
function require_featured_image_validation_36925( $data, $form_data ) {
.................
}

And the second one becomes:


add_filter( 'cred_form_validate', 'require_featured_image_validation_37147', 10, 2 );
function require_featured_image_validation_37147( $data, $form_data ) {
.................
}

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1342075
0.png
00.png

Thanks Waqar for your help . I have written the changes but have failed in the second custom code.

It has been updated in fragment "password-suggestion".
An error occurred while trying to rerun the snippet. Syntax error, unexpected '', 10, 2); ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' in /home2/fundtele/public_html/sg/wp-content/toolset-customizations/contrasena-sugerencia.php on line 10 A problem occurred when executing snippet "password-suggestion". The result of include_once is: ""

add_filter( 'cred_form_validate', 'require_featured_image_validation_37147'', 10, 2 );
function require_featured_image_validation_37147( $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;
}

In the first one I wrote the following;

add_filter( 'cred_form_validate', 'require_featured_image_validation_36925', 10, 2 );
function require_featured_image_validation_36925( $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']='La contraseña no es correcta';
}
$data =array($fields,$errors);
}
return $data;
}

#1342531

Hi Pedro,

I've noticed an extra ' in that code snippet.
( screenshot: hidden link )

Please update:


add_filter( 'cred_form_validate', 'require_featured_image_validation_37147'', 10, 2 );

To:


add_filter( 'cred_form_validate', 'require_featured_image_validation_37147', 10, 2 );

regards,
Waqar

#1343119

works perfectly, thanks Waqar

#1343607

Hi Pedro,

Thanks for the update and glad that it works.

You're welcome to mark this ticket as resolved and for a new question/concern, please open a new one.

regards,
Waqar