Skip Navigation

[Resolved] Cannot make date filed required in user form

This support ticket is created 6 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#516192

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]

#516254

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.

#517407

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'?

#517508

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.

#517857

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&gt;

#517966

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.