Skip Navigation

[Resolved] Unable to validate date with the cred_form_validate hook

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

Problem:
Use CRED filter hook cred_form_validate to valid a custom date field value
Solution:
Simply demo codes:

add_filter('cred_form_validate','my_version_validation',10,2);
function my_version_validation($field_data, $form_data) {
    list($fields,$errors) = $field_data;
    if ( 4 == $form_data['id'] ){
        if (intval($fields['wpcf-legue-date-de-fin1']['value']['datepicker']) >0) {
            $errors['legue-date-de-fin1']='Date de fin antérieure à la date de début';
        }
    }
    return array($fields, $errors);
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-api/#cfv

This support ticket is created 8 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by Jean-Philippe 8 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#358644

I am trying to check if the ending date precede the starting one.
I' used the hook like this:

function my_version_validation($field_data, $form_data) {
list($fields,$errors) = $field_data;
    if ( 4 == $form_data['id'] ){


        if (!empty( $fields['post_content'] ) && strlen($fields['post_content']['value']) > 350) {
            $errors['post_content'] = 'Le contenu de ce champ est trop long '.strlen($fields['post_content']['value']);

        }if ($fields['mettre-a-jour-mon-legue']['value'] !='1') {
            $errors['mettre-a-jour-mon-legue'] = 'Vous devez accepter de mettre à jour ma fiche personnelle';
        }
				if ( empty($fields['post_content']['value']) || $fields['post_content']['value']=="" ) {
						$errors['post_content']='Ce champs est obligatoire';
				}
				if (intval($fields['legue-date-de-fin1']['value']) < intval($fields['legue-date-de-debut1']['value'])) {
						$errors['legue-date-de-fin1']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['legue-date-de-fin2']['value']) < intval($fields['legue-date-de-debut2']['value'])) {
						$errors['legue-date-de-fin2']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['legue-date-de-fin3']['value']) < intval($fields['legue-date-de-debut3']['value'])) {
						$errors['legue-date-de-fin3']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['legue-date-de-fin4']['value']) < intval($fields['legue-date-de-debut4']['value'])) {
						$errors['legue-date-de-fin4']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['legue-date-de-fin4']['value']) < intval($fields['legue-date-de-debut4']['value'])) {
						$errors['legue-date-de-fin4']='Date de fin antérieure à la date de début';
				}

    }

    return array($fields, $errors);
}

tested both $fields['legue-date-de-debut1']['value'] and $fields['legue-date-de-debut1']
But not matter what I get null value for all the date fields.
The other validation work without problem.

#358767

Dear Jean,

Is the custom field "mettre-a-jour-mon-legue" created with Types plugin?
If it is, Types prepend "wpcf-" before the field slug, please try to modify your PHP codes to "wpcf-mettre-a-jour-mon-legue", and test again.

See the live example of our document:
cred_form_validate
https://toolset.com/documentation/user-guides/cred-api/#cfv

#358983

mettre-a-jour-mon-legue work as intented, my problem is with legue-date-de-fin1.

#359068

Same method, please try with slug "wpcf-legue-date-de-fin1"

#359294

Nope still getting null value

#359385

I just tested it in my localhost, for the custom date field which is created with Types plugin, you need to use it like this:
$fields['wpcf-legue-date-de-fin1']['value']['datepicker']

It is a timestamp value.

#360182

Ok validation seem to work but i can't display the error.

$errors['wpcf-legue-date-de-debut1']='Date de fin antérieure à la date de début';

won't work but

$errors['mettre-a-jour-mon-legue']='Date de fin antérieure à la date de début';

will display the error.

#360368

I can not duplicate same problem, since you did not paste the current codes, I tested with a simply one:

add_filter('cred_form_validate','my_version_validation',10,2);
function my_version_validation($field_data, $form_data) {
	list($fields,$errors) = $field_data;
    if ( 4 == $form_data['id'] ){
        if (intval($fields['wpcf-legue-date-de-fin1']['value']['datepicker']) >0) {
            $errors['legue-date-de-fin1']='Date de fin antérieure à la date de début';
        }
    }
    return array($fields, $errors);
}

It does return an error message when you select a value in date field "wpcf-legue-date-de-fin1"

Please check this:
If you are going to compare two date field values, please make sure you are using same array type like this:
$fields['wpcf-legue-date-de-fin1']['value']['datepicker']

#361055

ok So i updated my $error field and date validation now work

thx

function my_version_validation($field_data, $form_data) {
list($fields,$errors) = $field_data;
    if ( 4 == $form_data['id'] ){


        if (!empty( $fields['post_content'] ) && strlen($fields['post_content']['value']) > 350) {
            $errors['post_content'] = 'Le contenu de ce champ est trop long '.strlen($fields['post_content']['value']);

        }
				if ( empty($fields['post_content']['value']) || $fields['post_content']['value']=="" ) {
						$errors['post_content']='Ce champs est obligatoire';
				}
				if (intval($fields['wpcf-legue-date-de-fin1']['value']['datepicker']) < intval($fields['wpcf-legue-date-de-debut1']['value']['datepicker'])) {
						$errors['legue-date-de-debut1']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['wpcf-legue-date-de-fin2']['value']['datepicker']) < intval($fields['wpcf-legue-date-de-debut2']['value']['datepicker'])) {
						$errors['legue-date-de-fin2']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['wpcf-legue-date-de-fin3']['value']['datepicker']) < intval($fields['wpcf-legue-date-de-debut3']['value']['datepicker'])) {
						$errors['legue-date-de-fin3']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['wpcf-legue-date-de-fin4']['value']['datepicker']) < intval($fields['wpcf-legue-date-de-debut4']['value']['datepicker'])) {
						$errors['legue-date-de-fin4']='Date de fin antérieure à la date de début';
				}
				if (intval($fields['wpcf-legue-date-de-fin5']['value']['datepicker']) < intval($fields['wpcf-legue-date-de-debut5']['value']['datepicker'])) {
						$errors['legue-date-de-fin4']='Date de fin antérieure à la date de début';
				}
				if (!$fields['mettre-a-jour-mon-legue']['value']) {
            $errors['mettre-a-jour-mon-legue'] = 'Vous devez accepter de mettre à jour ma fiche personnelle';
        }

    }

    return array($fields, $errors);
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.