• Resolved maurotello73

    (@maurotello73)


    Hi! I made a validation through code so that 2 email fields are the same, when I press the send button it finds the error, but after modifying it it does not send me the form, it tells me that it is necessary to complete a field to be able to send it, and they are all complete. It’s like I lose my courage no matter how much I am seeing them.

    I removed the AJAX validation, to verify if that was the error, and in this case when it throws me the error that those 2 fields are not the same, the form is reloaded, but the input data appears, but the attachments are lost , I have to upload them again.

    It is because of this that I want to perform an inline validation, that is, that the validation of the 2 fields is performed when it loses the focus of the second field, which validates me at that time.

    How can I do inline validation with code?

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @maurotello73

    I hope you’re well today!

    If the goal here is to make sure that two e-mail fields are the same then why not use a bit different approach?

    Try this code as MU plugin:

    <?php 
    
    add_filter(
    'forminator_custom_form_submit_errors',
    function( $submit_errors, $form_id, $field_data_array ) {
    	
    $email_field = 'email-1';
    $email_compare_field = 'email-2';
    
    $email = isset( $_POST[ $email_field ] ) ? $_POST[ $email_field ] : false;
    $email_compare = isset( $_POST[ $email_compare_field ] ) ? $_POST[ $email_compare_field ] : false;
    
    if ( ! $email ) {
    return $submit_errors;
    }
    
    if ( $email !== $email_compare ) {
    	$submit_errors[][ $email_field_compare] = __( 'Email addresses are not the same.' , $domain );
    return $submit_errors;
    }
    },
    10,
    3
    );

    In these lines you’ll need to set correct IDs of fields to compare (as used in the form)

    $email_field = 'email-1';
    $email_compare_field = 'email-2';

    and then just:

    – create empty file with a .php extension (e.g. “forminator-email-field-validation.php”)
    – put the code into it
    – upload the file to the “/wp-content/mu-plugins” folder of the site.

    Best regards,
    Adam

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi again @maurotello73

    I’ve been reviewing some of my recent posts and realized that while sharing the code I mentioned that it’s a “different approach” but didn’t really tell why it’s different ??

    The main difference here is that it’s not triggered by “field focus change” (so when one field looses focus) but on form submission. This way it’s possible to hook to Forminator core and also makes it less prone to any scripts conflicts etc.

    The error message, however, would be presented the same way as any other “default” Forminator form errors so from “user’s point of view” it should be pretty much the same.

    Just thought I’ll add some explanation ??

    All the best,
    Adam

    Thread Starter maurotello73

    (@maurotello73)

    @wpmudev-support8
    Sorry in which folder? I have wp-content / forminator. Could you give me an email so I can explain you better?

    • This reply was modified 3 years, 5 months ago by maurotello73.
    Thread Starter maurotello73

    (@maurotello73)

    @wpmudev-support8

    I had put a similar code in the function but that’s when it throws the error .. as long as I have attachments loaded, it tells me that there is no field filled in, when in fact there are all required fields .. but it only happens to me when the error I add it in the validation of those emails, with the rest it works well for me. I pass the code that I have in the function, where I have other validations

    add_filter(‘forminator_custom_form_submit_errors’, ‘my_custom_form_validation’, 99, 3);

    function my_custom_form_validation($submit_errors, $form_id, $field_data_array) {

    // Skip if is not the correct form
    if( (int) $form_id !== 2643 ){
    return;
    }

    $fields = [];
    $fields[] = $_POST[’email-1′];
    $fields[] = $_POST[’email-2′];
    $conyuge = $_POST[‘checkbox-3’];
    $hijo1 = $_POST[‘checkbox-4’];
    $hijo2 = $_POST[‘checkbox-5’];
    $hijo3 = $_POST[‘checkbox-6’];
    $hijo4 = $_POST[‘checkbox-8’];

    if (isset($conyuge)){

    if ($_POST[‘name-2’] == null){
    $submit_errors[][‘name-2’] = __( ‘Campo obligatorio porque tiene activo cónyuge’ );
    }
    if ($_POST[‘number-2’] == null){
    $submit_errors[][‘number-2’] = __( ‘Campo obligatorio porque tiene activo cónyuge’ );
    }

    }

    if (isset($hijo1)){
    if ($_POST[‘name-3’] == null){
    $submit_errors[][‘name-3’] = __( ‘Campo obligatorio porque tiene activo primer hijo’ );
    }
    if ($_POST[‘number-3’] == null){
    $submit_errors[][‘number-3’] = __( ‘Campo obligatorio porque tiene activo primer hijo’ );
    }

    }

    if (isset($hijo2)){
    if ($_POST[‘name-4’] == null){
    $submit_errors[][‘name-4’] = __( ‘Campo obligatorio porque tiene activo segundo hijo’ );
    }
    if ($_POST[‘number-4’] == null){
    $submit_errors[][‘number-4’] = __( ‘Campo obligatorio porque tiene activo segundo hijo’ );
    }

    }

    if (isset($hijo3)){
    if ($_POST[‘name-5’] == null){
    $submit_errors[][‘name-5’] = __( ‘Campo obligatorio porque tiene activo tercer hijo’ );
    }
    if ($_POST[‘number-5’] == null){
    $submit_errors[][‘number-5’] = __( ‘Campo obligatorio porque tiene activo tercer hijo’ );
    }

    }

    if (isset($hijo4)){
    if ($_POST[‘name-6’] == null){
    $submit_errors[][‘name-6’] = __( ‘Campo obligatorio porque tiene activo cuarto hijo’ );
    }
    if ($_POST[‘number-6’] == null){
    $submit_errors[][‘number-6’] = __( ‘Campo obligatorio porque tiene activo cuarto hijo’ );
    }

    }

    // Check if both emails are the same
    /*
    if (count(array_unique($fields)) !== 1) {
    // Custom error
    $submit_errors[][ ’email-2′] = __( ‘Las casillas de Email no son iguales’ );
    }*/

    $email1 = $email2 = ”;
    foreach( $field_data_array as $arr ) {
    if( $arr[‘name’] == ’email-1′ ) $email1 = $arr[‘value’];
    if( $arr[‘name’] == ’email-2′ ) $email2 = $arr[‘value’];
    if( ” != $email1 && ” != $email2 ) break;
    }

    if( $email1 != $email2 ) {
    $submit_errors[][’email-2′] = __( ‘Los emails cargados no son iguales’ );
    }

    // Always return $submit_errors
    return $submit_errors;

    }

    When it enters that validation of the same emails it throws me the error, that is, the first error that it throws me is fine, it is telling me that the emails without different, so far perfect, but when I correct the emails and press send again .. There is where the second error appears that tells me that I must complete some of the fields .. it is like it loses the value of the fields already completed before the error of the same emails. At sight they are, that is, I can see that the fields are complete, but when I press SEND (after correcting the error in the mails) this second error jumps .. I reiterate for me it is like it loses the values of the fields

    • This reply was modified 3 years, 5 months ago by maurotello73.
    Plugin Support Predrag – WPMU DEV Support

    (@wpmudev-support1)

    Hi @maurotello73,

    The PHP file Adam mentioned should be placed inside wp-content/mu-plugins/ folder, if you don’t have mu-plugins folder simply create one yourself and add the PHP file with the code in there.

    You can read more about what an MU plugin is and how it works here:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Cheers,
    Predrag

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @maurotello73

    I hope you are doing well and safe!

    We haven’t heard from you in a while, I’ll mark this thread as resolved.

    Feel free to let us know if you have any additional questions or problems.

    Best Regards
    Patrick Freitas

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Create new inline validation’ is closed to new replies.