Remote validation for uniquenes - how to prevent when editing existing post?

Support General Remote validation for uniquenes - how to prevent when editing existing post?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #44364
    phillcoxonphillcoxon
    Participant

    I have a tramp CPT that contains a reference field that must be unique.

    I have created a remote validation for that field that checks for any other published post that contain the same reference and if so will display an error. The code is below and it works fine.

    add_action("wp_ajax_validate_unique_reference", "validate_unique_reference");
    
    function validate_unique_reference()
    {
        // Get the field value via the global variable $_GET
        if ($_GET["reference"]) {
    		
            $ref = $_GET["reference"];
    
            $args = [
                "post_type" => "tramp",
                "post_status" => "publish",
                "meta_query" => [
                    [
                        "key" => "reference",
                        "value" => $ref,
                    ],
                ],
            ];
    
            $query = new WP_Query($args);
    
            // Check for existing tramps with the reference $ref
            $count = $query->found_posts;
    
            if ($count > 0) {
    	    //echo "This reference has been used. Please choose another.";  // does not work - only returns false when using Metabox UI? 
                echo "false";
            } else {
                echo "true";
            }
    
            die();
        }
    }
    

    The problem I have is that if I edit an existing tramp post and try to save the validation will fail because the reference already exists in a published post - the one I am currently editing.

    Questions:

    1) How do I do validate this field while excluding the post that I am editing?

    The ideal solution would be to pass the post ID so that it can be excluded from the query:

    
            $args = [
                "post_type" => "tramp",
                "post_status" => "publish",
                "post__not_in" => array($post_id),
                "meta_query" => [
                    [
                        "key" => "reference",
                        "value" => $ref,
                    ],
                ],
            ];
    

    ... but I don't believe it's possible to retrieve the post it when doing remote validation?

    And yet the Metabox remote validation documentation suggests the possibility:

    The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.

    Is there any way to use remote validation to check for a unique value in a field while ignoring that reference in an existing post that it is being edited? Any way to pass the post ID of a page being edited?

    2) If I cannot use remote validation to check for a unique field value, can I use something like rwmb_before_save_post to do the check, prevent a new post with the same reference being published and show an error in the edit post screen?

    One advantage of rwmb_before_save_post is we will have the use of the current post being edited to exclude from the reference check.

    3) Can someone confirm that if I am using the Metabox UI to define custom fields (vs code) that it will ignore any custom validation failed error message?

    In the example above when I try to define a custom error message in the remote validation function:

    //echo "This reference has been used. Please choose another."; // does not work - only returns false when using Metabox UI?

    ...it never works.

    Anything other than true returns as a failure.

    I'm wondering if this is because the custom field UI remote validation configuration offers a space for a custom message if validation fails and hence anything we pass back from the remote validation function will always be ignored?

    Personally I think if the remote validation function provides a custom validation error message and the UI option is empty Metabox should alway show the custom message.

    Thank you!

    #44370
    PeterPeter
    Moderator

    Hello Phil,

    1. Currently, there is an issue with the remote validation and I cannot check if the post ID will work in this case.

    2. Yes, it may be a good approach. You can use that action hook and get the post ID via the global variable $_POST.

    3. No, the custom validation message in the builder works properly.

    #44376
    phillcoxonphillcoxon
    Participant

    Hi Peter,

    Thank you for the response.

    For:

    (2) are you able to offer any example of how I can prevent saving and return the user to the editor with an error message if using rwmb_before_save_post?

    (3) Let me clarify this. Would you confirm that if I am using the Metabox Builder and I try to return a custom validation error message via code from the validation function (above), that it will be ignored? Using the Metabox Builder prevents the validation error from being returned via code and only pays attention to the validation message in the Metabox Builder, even if that field is left blank?

    Thanks in advance!

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