I know how to set custom fields to be 'required' but I'm wondering is there a way to make a field 'required' on a Toolset form if the user selects a specific option from a radio field?
For example, say i have a radio field with options 'yes' and 'no' and a second field (let's call it 'my-custom-field'), how do I make 'my-custom-field' only required if the user selects 'yes' for the radio field?
I suspect this might be achievable using the cred validation hook but I'm not sure how to go about it. Can you provide some guidance please?
Thanks
It is not possible to add the requirement of a field dynamically listening to another field out of the box
What I do usually is hiding that field until another condition applies.
So, you can kind of have this, but with a workaround.
You would simply have 2 Fields, which basically store the same (and eventually with some Custom code applied to the Forms API even updates the original field).
One of both fields is required and the other not.
With a Forms condition you would hide each of them accordingly the other field, which you want to use as base to the condition.
The Doc that explains the Forms conditions is here:
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/
What's also possible is to add a generic field which is required, but then instead of using "required":0, or "required":1,. you use "required":[any ShortCode that returns 1 or 0, for example, a post field, or any other thing],
Note that you cannot nest ShortCodes there so wpv-conditional is not to be used (as it nests ShortCodes)
This, just works with the stored value (so not what you currently edit)
Can you let me know where exactly (on what level) you will need to divide upon required or not?
Maybe, you can solve that with a validation applied to the submit button as we did in past (if the field is not filled and another field's value is xy, hide the Submit Button of the form and display a message)
Please let me know about the exact plans in case above isn't complete.
Hi Beda
Thank you for providing a few ways of achieving this. It's always good to have some alternatives for different situations.
For my current requirement I've ruled out using a generic field as I only want the second field to display if the user selects 'yes' for the first field.
I've manged to achieve what I want by setting the first custom field as required and the second not required, using conditional display on the form to only show the second field if the user selects 'yes' in the first and using the following validation hook to make the second field required if the value of the first is 'yes':-
add_filter('cred_form_validate','validate_data',10,2);
function validate_data ( $field_data, $form_data ){
list($fields,$errors)=$field_data;
//validate if specific form
if ($form_data['id']==528) {
//check my-custom-field
if ( empty( $fields['wpcf-my-custom-field']['value'] ) ){
//set error message for my-custom-field
$errors['wpcf-my-custom-field'] = 'This field is required';
}
}
return array($fields,$errors);
}
I though I might have needed some assistance with writing the hook but turns out I didn't!
Thanks for your help.