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
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
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;
}
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
works perfectly, thanks Waqar
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