dateFormat in validator
dateFormat in validator
Struggling with the date format in the validator.
It lets me select the correct date format from the date picker, but when i press save i get: Date is not in the expected format
[code]{
"label": "Sent to:",
"name": "date_sent_to",
"type": "date",
"dateFormat": 'dd/mm/yy'
}
Field::inst( 'date_sent_to' )->validator( 'Validate::required' )
->validator( 'Validate::dateFormat', 'dd/mm/yy' ),
[/code]
It lets me select the correct date format from the date picker, but when i press save i get: Date is not in the expected format
[code]{
"label": "Sent to:",
"name": "date_sent_to",
"type": "date",
"dateFormat": 'dd/mm/yy'
}
Field::inst( 'date_sent_to' )->validator( 'Validate::required' )
->validator( 'Validate::dateFormat', 'dd/mm/yy' ),
[/code]
This discussion has been closed.
Replies
Regards,
Allan
[code] {
"label": "Sent to:",
"name": "date_sent_to",
"type": "date",
"dateFormat": 'dd/mm/yy'
}
Field::inst( 'date_sent_to' )->validator( 'Validate::required' )
->validator( 'Validate::dateFormat', 'd/m/y' ),
[/code]
The difference between the two at the moment is in jQuery UI `yy` means "4 digit year", while in PHP "y" means 2 digit year. If you want 4 digit year in PHP use `Y` :
http://php.net/date
http://api.jqueryui.com/datepicker/ (formatDate function)
Allan
Regards,
Allan
One more thing, im using this:
[code]Field::inst( 'date_pub' )->validator( 'Validate::dateFormat', 'd/m/Y' )[/code]
it is not a required field, but when i leave it blank and save the form it comes up with: Date is not in the expected format
[code]
public static function dateFormat( $val, $data, $opts ) {
$format = is_array($opts) ? $opts['format'] : $opts;
if ( $val === '' ) {
return true;
}
$date = date_create_from_format($format, $val);
if ( ! $date ) {
return isset( $opt['message'] ) ?
$opts['message'] :
"Date is not in the expected format";
}
return true;
}
[/code]
and that will address the issue.
Regards,
Allan
One is required and the other is not.
Therefore if [quote]$val[/quote] equals nothing is conflicting with the other date field.
[code]
if ( $val === '' ) {
return true;
}
[/code]
Regards,
Allan
The following got it working:
[code] Field::inst( 'date_sent_to' )->validator( 'Validate::required' )
->validator( 'Validate::dateFormat_required', 'd/m/Y' ),[/code]
@version 1.2.4 does not have the lib/php files inside ?
Or it's me that I'm missing something ?
Thanks!