dateFormat in validator

dateFormat in validator

mbrennandmbrennand Posts: 34Questions: 4Answers: 0
edited October 2013 in Editor
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]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The date formatter uses PHP's date() formatting options ( http://php.net/date ), so `dd` actually means "Day of the month, 2 digits with leading zeros" twice! I think you probably want: `d/m/y` .

    Regards,
    Allan
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    ok so i tried this but still comes up with: 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', 'd/m/y' ),
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Its a bit frustrating working with dates - I know! Unfortunately jQuery UI and PHP use different syntax:

    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
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    thanks, although this now works it allows me to type for example 01/01/13 or 01/01/2013 i only want the four digit year. can i disable the typing for the date input?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I'm not aware of an option in jQuery UI to allow that I'm afraid (looking through their documentation here: http://api.jqueryui.com/datepicker/ ). Possibly it might be best to ask in the jQuery UI discussion group.

    Regards,
    Allan
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    Ok thanks, ill try to resolve and post back i find anything.

    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
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    That's a bug in the 1.2.3 package I'm afraid. I'm going to be releasing 1.2.4 shortly (today or tomorrow is the plan) which will address this issue, but until then, the fix is actually relatively simple. If you look in `php/lib/Editor/Validate.php` right at the bottom of the file you'll find the `dateFormat` method. Replace what is there with:

    [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
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    Thanks, although this works i have another date field on the same form.
    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]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Are you using `dateFormat` and `dateFormat_required` ? The latter is a PHP "magic" function which should enforce the fact that the value is required. When using the `_required` function, it will first check to see if the value is present, before running the specific validation function - so it actually shouldn't get as far as that check if it is required.

    Regards,
    Allan
  • mbrennandmbrennand Posts: 34Questions: 4Answers: 0
    Brilliant, thank you very much.
    The following got it working:

    [code] Field::inst( 'date_sent_to' )->validator( 'Validate::required' )
    ->validator( 'Validate::dateFormat_required', 'd/m/Y' ),[/code]
  • AKM3AKM3 Posts: 9Questions: 0Answers: 0
    Hey Allan, question:

    @version 1.2.4 does not have the lib/php files inside ?

    Or it's me that I'm missing something ?

    Thanks!
This discussion has been closed.