Home › Toolset Professional Support › [Resolved] Unable to validate fields anymore via cred_form_validate
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
This topic contains 10 replies, has 2 voices.
Last updated by Minesh 2 years, 10 months ago.
Assisted by: Minesh.
I am facing these strange issues where I am not able to validate the form anymore using cred_form_validate but was able to do that earlier.
function hiring_goals_mandatory_taxonomy($field_data, $form_data) { // Split $field_data into separate $fields and $errors list( $fields,$errors ) = $field_data; // validate specific form if ( $form_data['id'] == 50398 ) { // check if role archetype is set if ( empty( $fields['role-archetype']['value'][0] ) ) { $errors['role-archetype'] = 'You must choose a role archetype'; } // check if employment type is set if ( empty( $fields['employment-type']['value'][0] ) ) { $errors['employment-type'] = 'You must choose an employment type'; } // check if job seniority level is set if ( empty( $fields['job-seniority-level']['value'][0] ) ) { $errors['job-seniority-level'] = 'You must choose a job seniority level'; } return array($fields,$errors); } } add_filter( 'cred_form_validate', 'hiring_goals_mandatory_taxonomy', 10, 2 );
Video - hidden link
What am I missing here? This is happening across the board on all forms for all custom post types wherever I have a validation test.
Hello. Thank you for contacting the Toolset support.
Based on the video you shared and when I see the custom code snippet you added to "Custom Code" section, I can see there is error.
When you see the code snippet, at bottom of the code snippet you added you added closing PHP tag ?> , can you please remove that and save code snippet and make sure when you save it there is no Error shows with code snippet and try to resolve your issue.
Minesh,
I tried to do exactly what you said and indeed the error in the custom code is gone but the code is still throwing an error on the form even if the fields are not empty.
New video - hidden link
That is strange. Can you please make sure that you did not added any other code snippet that belongs to the form you are editing.
Can you please share problem URL where I can see the issue as well as admin access details.
I have set the next reply to private which means only you and I have access to it.
I've adjusted the validation hook code as given under:
function hiring_goals_mandatory_taxonomy($field_data, $form_data) { // Split $field_data into separate $fields and $errors list( $fields,$errors ) = $field_data; // validate specific form if ( $form_data['id'] == 50398 ) { // check if role archetype is set //if ( empty( $fields['role-archetype']['value']) ) { if ( empty( $_POST['role-archetype']) ) { $errors['role-archetype'] = 'You must choose a role archetype'; } // check if employment type is set //if ( empty( $fields['employment-type']['value'] ) ) { if ( empty( $_POST['employment-type']) ) { $errors['employment-type'] = 'You must choose an employment type'; } // check if job seniority level is set // if ( empty( $fields['job-seniority-level']['value'] ) ) { if ( empty( $_POST['job-seniority-level']) ) { $errors['job-seniority-level'] = 'You must choose a job seniority level'; } return array($fields,$errors); } } add_filter( 'cred_form_validate', 'hiring_goals_mandatory_taxonomy', 10, 2 );
Can you please confirm it works as expected.
Minesh,
That worked just fine. A few questions -
1. Why did this happen when similar code was working before? Was there a change in logic for fields[ ]?
2. I have other validations where I am using fields[ ] to validate data but they are also not working. Do I need to change all of these?
3. How does this solution vary for a custom field vs a taxonomy? $_POST['slug'] should work across the board irrespective of taxonomy or field. Right?
Before I run few other test I will require some information:
- When you created the form, have you set it to submit without using AJAX and later you update the form to submit using AJAX?
- Do you see the $fields works only with non-ajax submit form?
The form does not have AJAX submit enabled and I did not make the change either.
$fields is not working with non ajax form
I'm not sure why its not working on your site there might be some conflict between the theme or plugin you are using.
I've run a test on both my localhost machine with clean installation where only Toolset plugins and twentytwentyone theme active and I run a test with both post Add and post Edit form and I can see with both form the hook "cred_form_validate" does works as expected and the $fields also holds the correct values.
I've created the following test site, you can login using the following link:
- hidden link
Here you can find Add form:
- hidden link
And with following link you can find edit form (this form uses AJAX so you will require to check ajax response to see the validation hook content):
- hidden link
For both form I've added the "cred_save_data" hook to run:
- hidden link
And when I try to submit the form I can see it prints the expected values with $fields as well as $form_data variables. I suspect there must be something that overrides the form submit, you may would like to disable all non-Toolset plugins and run a test and activate the non-Toolset plugins one by one to find the cause.
Thanks, Minesh.
I have a plugin called Plugin organizer that disables some plugins on page load to increase site speed. But, the toolset plugins are enabled across the board.
I have a few followups -
1. Does the $_POST['field-name'] approach work only for AJAX fomr submission or does it work always?
2. Can you tell me which plugin contains custom code written in settings--> custom tab in the toolset?
3. Are there known cases of incompatibility with other plugins with Toolset that I should consider?
1. Does the $_POST['field-name'] approach work only for AJAX fomr submission or does it work always?
==>
It will work for both AJAX and non-AJAX forms.
2. Can you tell me which plugin contains custom code written in settings--> custom tab in the toolset?
==>
The "Custom Code" section is added with Toolset Types plugin.
3. Are there known cases of incompatibility with other plugins with Toolset that I should consider?
==>
No, I do not see any other report yet related to this issue.