I am trying to make a date field in the CRED user edit form below required. But required="true" won't work for some reason. The only way I have been able to do it is to make the field required in the user field group. This method isn't good because I don;t want the field to be required whenever the user is edited, only when it is edited using the form I have created.
[creduserform class='cred-user-form cred-keep-original']
[cred_field field='form_messages' value='']
<div class="cred-field cred-field-first_name">
[wpv-user field="user_firstname"] [wpv-user field="user_lastname"]
</div>
[cred_generic_field field='Confirm' type='checkbox' class='' urlparam='']
{
"required":1,
"validate_format":0,
"checked":0,
"default":"1",
"label":""
}[/cred_generic_field] I confirm I have read and understand the Presenter Manual, Presenter Promise and Broadcast Code
[cred_show_group if="($(Confirm) eq '1' )" mode='fade-slide']
Date
[cred_field field='presenter-compliance-confirmed' post='user' value='' urlparam='' required="true"]
[cred_field field='form_submit' value='Submit' urlparam='']
[/cred_show_group]
[/creduserform]
Hi, you can enforce this requirement using server-side validation. Please add the following code to your functions.php file:
// require date when form is edited
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;
//validate if specific form
if ($form_data['id']==53)
{
//check my_field value
if ($fields['wpcf-mydatefieldslug']['value'][0] == '')
{
//set error message for my_field
$errors['wpcf-mydatefieldslug']='Date is required';
}
}
//return result
return array($fields,$errors);
}
Change the "53" to match the ID of your edit form, and change 'mydatefieldslug' to match the slug of your date field. Please try this and let me know the results.
Is this the only way to do this? Is this specific to the date field? i thought it was possible on CRED forms to use required='true'?
If you add the date field using the cred_field shortcode, you don't have the ability to use required="true". It's not included in the list here, so it's not a supported attribute for this shortcode:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
However, you could create a generic date field and apply the required attribute to that field. Then you could use a cred_save_data hook to capture that generic date value and update your custom field value on the backend. It's a bit more PHP work, but it implements front-end validation.
Thanks. I'm confused; why does this support ticket say it is possible to add required="true" to a form field?
<a href="https://toolset.com/forums/topic/cred-how-to-make-fields-required/">https://toolset.com/forums/topic/cred-how-to-make-fields-required/</a>
That's confusing, sorry for that. As Caridad mentions in this comment:
https://toolset.com/forums/topic/cred-how-to-make-fields-required/#post-195160
..the required attribute is not supported for all fields. The initial test with post title worked, and that was why this solution was suggested. After that, you can see lots of custom code people put in place to work around the required attribute.