Remote Validation Example?

Support General Remote Validation Example?Resolved

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #22933
    Mitchell ValkMitchell Valk
    Participant

    Hi,

    I'm currently working on some remote validation and I sort of got it working with just the admin ajax url, but I'm now wanting to get more control over the data that is being send to the ajax function. Currently I can only access the field on which the validation is being done on, I want to check two fields at the same time.

    Is there some kind of example somewhere how I can use the more advanced remote settings as described in the documentation?

    Thanks!

    #22934
    Mitchell ValkMitchell Valk
    Participant

    For other people trying to initially find out how the remote function works, since I had some issue finding this out initially, here's some sample code.

    'validation' => array(
        'rules' => array(
            'field_id' => array(
                'remote' => admin_url( 'admin-ajax.php?action={action_name}' ),
            ),
        ),
    ),
    add_action( 'wp_ajax_{action_name}', 'remote_validation' );
    
    function remote_validation() {
        echo wp_json_encode( 'true' );
        return true;
    }
    #22957
    Long NguyenLong Nguyen
    Moderator

    Hi Mitchell,

    The validation is being checked immediately after you click outside the input box. You can check more fields by adding more rules. One action name should be checked for one field ID.

    'validation' => array(
        'rules' => array(
            'field_id1' => array(
                'remote' => admin_url( 'admin-ajax.php?action={action_name1}' ),
            ),
            'field_id2' => array(
                'remote' => admin_url( 'admin-ajax.php?action={action_name2}' ),
            ),
        ),
        'messages' => array(
            'field_id1' => array(
                'remote'  => 'value is not passed',                    
            ),
        )
    ),
    add_action( 'wp_ajax_{action_name1}', 'remote_validation' );
    
    function remote_validation() {
        if( $_GET['field_id1'] === 'something' ) {      
            echo "true"; //valid
        } else {
            echo "false"; //invalid
        }
        die();
    }

    For more information, please follow the documentation
    https://jqueryvalidation.org/remote-method/
    https://www.sitepoint.com/jquery-ajax-validation-remote-rule/

    Note: validation remote seems does not work with Block Editor, please use the Classic Editor to use the custom validation. I will create a feature request for the developer team to support Block Editor.

    #22960
    Mitchell ValkMitchell Valk
    Participant

    Thanks for the response, the thing I'm trying to do is check if a post exists already that has 1 or 2 exact values. So to be more specific, I'm adding a chapter, and a chapter can have a chapter_number and chapter_part meta, so I'm already checking if the chapter_number is unique, but if chapter_part is also set, it needs to look for a post that has both metas set and if it exists, return false.

    So far all my attempts to get the data of 2 fields in one remote validation have not worked.

    #23134
    Anh TranAnh Tran
    Keymaster

    Hi Mitchell,

    I think it's doable since the remote method of jQuery validation allows you to send multiple data. Here's what I have in mind (not tested):

    rules: {
        chapter_number: {
            remote: {
                url: "admin-ajax.php?action=validate_chapter_number",
                type: "post",
                data: {
                    chapter_number: function() { // Get chapter_number value
                        return $("#chapter_number").val();
                    },
                    chapter_part: function() { // Get chapter_part value
                        return $("#chapter_part").val();
                    }
                }
            }
        }
    }
    #30625
    FedeFede
    Participant

    Is it possible to use remote validation from the MB Builder interface? Which URL should I specify?
    I'm trying this way and I can't get it to work:

    MB Builder

    add_action('wp_ajax_check_descripcion', 'ks_validate_descripcion');
    function ks_validate_descripcion()
        {
            echo 'false';
            die();
        }
    #30640
    Long NguyenLong Nguyen
    Moderator

    Hi Fede,

    The remote value in the Builder should be the full site URL.

    https://your-site.com/wp-admin/admin-ajax.php?action=check_descripcion

    #30649
    FedeFede
    Participant

    I have set the full URL in the callback and it works, but only partially.

    When I submit the form, the error message indicated in the MB Builder below the field appears briefly, but the validation continues and the post is saved.

    What am I doing wrong, how do I stop the validation when I reach this field?

    #30662
    Long NguyenLong Nguyen
    Moderator

    Hi Fede,

    I've created a note for this case on this reply https://support.metabox.io/topic/remote-validation-example/#post-22957

    Note: validation remote does not work with Block Editor, please use the Classic Editor to use the custom validation.

    #30665
    FedeFede
    Participant

    I expose my complete casuistry because it still does not work:

    I have a custom post type with no editor support.
    https://imgur.com/a/RnZBC4c
    Associated to that CPT I have a group of fields, one of the fields is a WYSIWYG editor with the ID 'post_content'.
    https://imgur.com/a/3HPGWA2
    In the code of a plugin I have the AJAX call to action:

    add_action('wp_ajax_check_descripcion', 'ks_validate_descripcion');
    
    function ks_validate_descripcion()
        {
            echo 'false';
            die();
        }
    

    When I submit the frontend form, the validation occurs and I can see the error message set in the MB Builder 'description failure', but the form submission continues its execution and ends up saving the post.

    If the CPT doesn't have editor support, I'm not using the block editor, am I?

    How can I make the validation stop?

    Thanks.

    #30667
    Long NguyenLong Nguyen
    Moderator

    Hi Fede,

    Thank you for your feedback.

    The remote validation does not look works with the Frontend Submission extension. You can try to use the Frontend Validation to validate the field value. Please get more details on this solution https://docs.metabox.io/extensions/mb-frontend-submission/#validation

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.