Mohammed H
Mohammed H

Reputation: 7048

Date validation not working for the date format 'd-M-Y'

In my CakePHP-1.2 application, I am using the date format 01-Jan-2012

Which date validation rule should I use to test it?

I tried array('date', 'dMy') . But it is not working.

Upvotes: 1

Views: 646

Answers (1)

Barry Chapman
Barry Chapman

Reputation: 6780

By reading the book, you can see that you cannot use separators in the date validation field for the algorithm you have selected. You will need to create a custom validation rule. You can do this using a Custom Regular Expression rule:

'/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\-(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\-((1[6-9]|[2-9]\\d)\\d{2})$/';

Note: This rule is a modified version of the canned ones that cake is shipped with.

So, you would want to do:

var $validate = array(
    'born' => array(
        'rule' => '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\-(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\-((1[6-9]|[2-9]\\d)\\d{2})$/i', 
        'message' => 'Enter a valid date in Day-Mon-Year format.'
    )
);

Note: the 'i' at the end of the regular expression, this simply denotes that it is a Case Insensitive match. This will match 24-Dec-2006, 24-DEC-2006, and 24-dec-2006 alike. Remove it if you want to accept the case sensitive match.

Now, you can simply run that rule against the field in question, and you should be good to go.

Good luck.

Upvotes: 3

Related Questions