black friday sale

Custom Fields
Re-Envisioned
Support General Remote Validation Example?

  • Creator
    Topic
  • #22933
    Resolved Mitchell 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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #22934
    Mitchell 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 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 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 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();
                    }
                }
            }
        }
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.