Skip Navigation

[Resolved] Custom Form Validation

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to add some custom validation to a Form with two custom date fields. I would like to validate it such that the start date is before the end date. How can I access the date value in the $error_fields parameter?

Solution: Use the following syntax to access the Unix timestamp for a datepicker field:

add_filter('cred_form_validate','start_end_date',10,2);
function start_end_date($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
...
$fields['wpcf-date-start']['value']['datepicker']

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/

This support ticket is created 4 years, 10 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 2 replies, has 2 voices.

Last updated by hui-suanC 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1280165

Tell us what you are trying to do?

> I am trying to validate a datetime field, such that the end date must be later than the start date

Is there any documentation that you are following?

https://toolset.com/documentation/programmer-reference/cred-api/

This is the code which I am using:

add_filter('cred_form_validate','start_end_date',10,2);
function start_end_date($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
// print_r($fields);

//validate if the form is for Creating or Updating an event
if ($form_data['id']==63 || $form_data['id']==76)
{
//check that T and C is checked value
if ($fields['wpcf-agree']['value']!=1)
{
//set error message for my_field
$errors['wpcf-agree']='You must agree to the Terms and Conditions';
}

// I am not sure whether to use 'value' or datepicker, but the end date MUST be later than the start date
if ($fields['wpcf-date-start']['datepicker'] > $fields['wpcf-date-end']['datepicker'])
{
//set error message for date-start
$errors['wpcf-date-start']='Start date must be before the end date.';
}

}
//return result
return array($fields,$errors);
}

#1280351

Hi, you can access the Unix timestamp of the selected date like this:

$fields['wpcf-date-start']['value']['datepicker']

If nothing is selected, this value will be empty.

#1284751

My issue is resolved now. Thank you!

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