Tuesday, February 5, 2013

Custom Format for Date Validator

The Date validator, when created with no 'format' option, will default to 'yyyy-mm-dd', or as a PHP format string, 'Y-m-d'. If you want to use a custom date format, the Date validator doesn't seem to be quite adequate. It relies on PHP being able to create a valid date with

DateTime::createFromFormat($format, $value)

It will also blindly validate any pure integer inputs, assuming no error is thrown when attempting to create a date with that integer as a timestamp. So, if you want a date to be in the format (mm-dd-yyyy), you would use the format string 'm-d-Y', but there are a lot of invalid dates that will still pass. For example, if someone enters '12-12-99', it will validate. While it is easy for a human to look at that and know that they probably meant 1999, your date parser might not handle it well (what you do with the validated date is up to you). For proper input validation in a case like this, you really need a regex validator (the default format checks against a regex behind the scenes, but not for custom formats).
                'validators'=>array(
                    array(  
                        'name'=>'Date',
                        'break_chain_on_failure'=>true,
                        'options'=>array(
                            'format'=>'m-d-Y',
                            'messages'=>array(
                                'dateFalseFormat'=>'Invalid date format, must be mm-dd-yyy', 
                                'dateInvalidDate'=>'Invalid date, must be mm-dd-yyy'
                            ),
                        ),      
                    ),      
                    array(  
                        'name'=>'Regex',
                        'options'=>array(
                            'messages'=>array('regexNotMatch'=>'Invalid date format, must be mm-dd-yyy'),
                            'pattern'=>'/^\d{1,2}-\d{1,2}-\d{4}$/',
                        ),      
                    ),      
                ),   
This particular regex will work with 1 or 2 digit days and months, because when I parse the date while handling the posted form data, I accept that format as well.
Note that I set a few custom error messages. 'dateInvalidDate' should display when a date like 13-13-1999 is entered, while the other message should show up for anything that doesn't match the specified format. 'break_chain_on_failure' is needed to prevent duplicate errors from displaying when the format is invalid.

5 comments:

  1. Thanks for sharing! We can provide you the best in class premium assistance for your SBCGlobal Mail account. Our solutions can enable you to log in to your account successfully, password recovery, password reset, fix the issues with your email account, etc.

    ReplyDelete
  2. The Eight-Wheel Classic - TITIAN Arts
    The eight-wheel classic bicycle is 바카라 사이트 available in six sizes. The Bicycle gri-go.com Wheel is a kadangpintar classic bicycle made in https://octcasino.com/ USA, but there are three variations in microtouch solo titanium

    ReplyDelete
  3. My Fair, Is My Fair, Is My Fair, is My Fair Casino?
    I'm https://febcasino.com/review/merit-casino/ an online 출장안마 casino and have been 바카라 사이트 playing at Fair since 2018 and 출장마사지 it's still not getting better. I am a young and goyangfc.com inexperienced gambler,

    ReplyDelete
  4. When these games first got here out, gamers have been very distrustful of them; without the spinning reels, it appeared like the games have been rigged. Even though the reels and handles in trendy machines are utterly irrelevant to the outcome result} of the game, manufacturers usually embrace them just to give 원 엑스 벳 gamers the illusion of management. With microprocessors now ubiquitous, the computer systems inside trendy slot machines permit manufacturers to assign a different probability to each symbol on every reel.

    ReplyDelete
  5. She has a Bachelor's in Screenwriting and experience in marketing and producing documentary movies. With her background as a buyer assist agent within the gambling business, she brings valuable insight to the Gambling News writers’ group. Foreign nationals 온라인카지노 in the country are unable to file formal complaints outcome of|as a outcome of} they can be forcibly removed from the country. Therefore, the criminal groups entice overseas employees, worldwide students and others into gambling illegally by saying that they will make massive sums of money by way of overseas messengers.

    ReplyDelete