Remote Validation Example?
- This topic has 10 replies, 4 voices, and was last updated 3 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
November 20, 2020 at 4:17 AM #22933
Mitchell Valk
ParticipantHi,
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!
November 20, 2020 at 4:23 AM #22934Mitchell Valk
ParticipantFor 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; }
November 21, 2020 at 7:53 AM #22957Long Nguyen
ModeratorHi 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.
November 21, 2020 at 8:58 AM #22960Mitchell Valk
ParticipantThanks 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.
December 1, 2020 at 3:07 PM #23134Anh Tran
KeymasterHi 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(); } } } } }
September 6, 2021 at 6:01 PM #30625Fede
ParticipantIs 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:add_action('wp_ajax_check_descripcion', 'ks_validate_descripcion'); function ks_validate_descripcion() { echo 'false'; die(); }
September 7, 2021 at 8:33 AM #30640Long Nguyen
ModeratorHi 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
September 7, 2021 at 4:39 PM #30649Fede
ParticipantI 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?
September 8, 2021 at 9:45 AM #30662Long Nguyen
ModeratorHi 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.
September 8, 2021 at 3:04 PM #30665Fede
ParticipantI 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.
September 8, 2021 at 8:55 PM #30667Long Nguyen
ModeratorHi 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
-
AuthorPosts
- You must be logged in to reply to this topic.