Can you split this ticket, I've another issue with Validation, but I'd like you go on with it as you already knows the wp-admin setup if needed.
I've add and edit Timeslots forms. Both have the Bus parent field to connect the timeslot with a Bus.
What I need is to validete it to be sure it is not empty.
I tried this code, but it doesn't work for me:
add_filter('cred_form_validate','my_form_validate',10,2);
function my_form_validate ($field_data, $form_data) {
list($fields,$errors)=$field_data;
if ($form_data['id']==217) {
$bus = $_POST['@bus-timeslot_parent'];
if (empty($bus))
{
log_me('Empty Bus');
$errors['@bus-timeslot_parent'] = 'You have to select a Bus';
} else {
log_me('NO Empty Bus');
}
}
return array($fields,$errors);
}
What I would like to achive is that the user cannot submit the form if the parent field is not set. Another complications is that I have many forms on the same page.
Hello. Thank you for contacting the Toolset support.
The wp-admin access details you shared with the previous ticket is not working with I try to login as toolset user. Can you please send me that so I can troubleshoot this issue further.
Ok - I've checked on your install and I found that you added the form to some popup.
It seems you are working on the site when I currently working. This will not help and take more time to troubleshoot the issue further.
First of all, I suggest you should separate the code you added. I've created another code snippet "toolset forms hook" where you should add all forms related hook and remove it from your "custom-functions" code snippet. You should reply here when you stop working on the site and I will start to investigate further.
Actually, Toolset form submits the data using PHP, this means that once you submit the form your page needs to refresh in order for the validations to run and return the errors.
I see you added the form to popup and form is set to submit so it will submit the form and refresh the page.
I suggest, to add some JS/jQuery code to make the parent field required and based on that you should enable and dislable the submit button that will be a really easy way to do so.
Thaks for your answer. Now we stop working on the website. I moved all the forms code to "toolset forms hook".
I like your suggestion about validate by jQuery. The difficulty is how to work on select2 select and that we have many forms on the same page. Can you help me with a jQuery example?
To implement the jQuery it needs custom code that is above and beyond the scope of our support policy.
However, I've added the following code to your form's JS box that will actually help you to locate the ID of your HTML select field. You can take it as reference:
jQuery(document).ready(function($){
id = $('select[name="@bus-timeslot.parent"]').prop('id');
});