Home › Toolset Professional Support › [Assigned] Validation error messages do not disappear
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
This topic contains 5 replies, has 1 voice.
Last updated by Gavin 19 hours, 31 minutes ago.
Assisted by: Christopher Amirian.
Hello
I have a very simple form to create a CPT. It is not using AJAX to submit the form.
I have replaced the submit button code with my own bootstrap button.
e.g.
<button class="btn btn-yellow wpt-form-submit submit" id="cred_form_1436_1_1_form_submit_1" name="form_submit_1">
Submit
</button>
The post_title field uses the default validation to check for an empty field. In the front end this displays the two form messages:
1. The post could not be created
2. This field is required.
Then when you click anywhere on the form, the first validation form message disappears, and when you start typing in the post_title field area the second validation form message disappears. This is all good and expected.
I have used some custom code to validate if a profile title already exists and used the code from this thread here:
https://toolset.com/forums/topic/issue-with-duplicate-post-titles-when-editing/#post-1823283
This all works, however, the error messages for my custom validation stay on the screen or do not seem to be 'cleared' if I start to type or resubmit the form. How can I get them to disappear or be cleared in the same way that the default ones do?
Many Thanks
Hi,
Welcome to Toolset support. What you’re seeing is expected when the Toolset submit control is replaced by a raw <button>. Toolset Forms wires its client-side validation and message handling to its own submit control and form markup. If you remove the mandatory submit shortcode, some of that behavior (like clearing messages automatically) won’t run. The same applies to the form messages placeholder.
Here are my suggestions:
1) Put back the mandatory shortcodes (then style them)
Inside the form markup, keep both of these:
[cred_field field='form_messages'] [cred_field field='form_submit' value='Submit' class='btn btn-yellow wpt-form-submit submit']
form_messages prints the container Toolset uses to show and then clear messages.
form_submit is the actual submit control Toolset hooks into.
You can add any Bootstrap/utility classes to it, so it looks identical to your custom button. (You can also wrap it in your own markup if you need buttons aligned a certain way.)
If you prefer a
<button>
element, you can wrap the shortcode:
<div class="btn btn-yellow">[cred_field field='form_submit' value='Submit']</div>
But don’t remove the shortcode itself.
For more information:
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/
2) Make your custom validation attach to the field key
When you validate for duplicate titles, return the error keyed to the field name (e.g. post_title). Toolset then shows an inline field error that clears when the user edits the field again.
add_filter( 'cred_form_validate', function( $error_fields, $form_data ){ // Only for this form (optional) if ( (int) $form_data['id'] !== 1436 ) return $error_fields; list( $fields, $errors ) = $error_fields; $title = isset( $fields['post_title']['value'] ) ? trim( $fields['post_title']['value'] ) : ''; if ( $title !== '' && title_already_exists_somehow( $title ) ) { // Attach to the field so it clears on edit $errors['post_title'] = 'This title already exists. Please choose another.'; } return array( $fields, $errors ); }, 10, 2 );
This uses the cred_form_validate hook. I did not test it. It is just for you to start investigating.
Hook documentation: https://toolset.com/documentation/programmer-reference/cred-api/
Thanks.
Thanks Christopher. I will investigate.
I replaced the default CRED fields:
[cred_field field='form_messages']
[cred_field field='form_submit' value='Submit' class='btn btn-yellow wpt-form-submit submit']
Yet the post Title field did not clear and the error message remained.
---
Also FYI:
$errors['post_title'] = 'This title already exists. Please choose another.';
does not appear inline with the post title. It may do for custom fields?
---
The problem happens when I put in a duplicate title.
The custom error message (duplicate field) appears correctly.
Then if I clear the post_title field and submit, the default error message appears (field required) BUT the custom message (duplicate field) remains with the message and there are 2 validation errors instead of 1.
Hi,
I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restore.
Make sure you set the next reply as private.
Also tell me which page to check the behavior.
Thanks.
Hi. I am going to see if I can replicate the problem on a clean install, then message back to give you access.