Support Forum
Support › MB Frontend Submission › rwmb_frontend_validate and custom fields with Ajax Issues
Hi
I have created a form with custom fields which need to be checked via PHP using a filter.
When I fill in the form and leave one of the custom fields empty to test if the validation filter works I find that the whole form does not display upon submission?
screen recording with ajax enabled
https://drive.google.com/file/d/1Hm7wMFNuAzC-4qzHqfKpMwxve4BpcXlR/view?usp=sharing
shortcode with ajax enabled
[mb_frontend_form id="product" confirmation="Your post has been successful" ajax="true"]
This is the filter check for the field 'start_date'.
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) {
if ( 'product' !== $config['id'] ) {
return $validate;
}
if ( empty( $_POST['start_date'] ) ) {
$validate = 'Please correct the errors highlighted below and try again.'; // Return a custom error message
}
return $validate;
}, 10, 2 );
Also when i test this with Ajax disabled in the shortcode then the form does display after submission but all of the fields are empty and does not keep the current input values? so the user will have to re-input all of the data again.
[mb_frontend_form id="product" confirmation="Your post has been successful"]
screen recording with no ajax
https://drive.google.com/file/d/12zzQxGJBaksg0vSLQKDfWZb4SI5U3Re5/view?usp=sharing
Hi,
It's the default behavior of the frontend form validation because it is validated on the backend (server), all the input values will be cleared.
You can use field validation to use JS validate with the setting remote
to create a request for server-side validation (Ajax). Read more on the documentation
https://docs.metabox.io/validation/
and refer to this topic https://support.metabox.io/topic/remote-validation-example/
Hi
ok for the remote validation that is fine.
But for the first query in regards to the whole form disappearing after form submission. Why is this? surely the form should remain there?
screen recording with ajax shortcode enabled
https://drive.google.com/file/d/1Hm7wMFNuAzC-4qzHqfKpMwxve4BpcXlR/view?usp=sharing
Thanks
Hi also I just checked the links and I cannot use remote validation due to that I am using the block editor, I cannot switch to the classic editor due to my site being built on the block editor.
Note: validation remote does not work with Block Editor, please use the Classic Editor to use the custom validation.
So my problem above is still an issue as the whole form disappears
screen recording with ajax shortcode enabled
https://drive.google.com/file/d/1Hm7wMFNuAzC-4qzHqfKpMwxve4BpcXlR/view?usp=sharing
Hi,
The form disappears when the validation is not passed if you enable ajax. I will inform the development team to consider improving this case. Thanks.
Hi
ok thanks, but the forms are useless without ajax and using custom fields with no jQuery validation. I know this is a rare case but the user experience is going to be bad if I cant validate all my forms without it disappearing.