Thursday, 24 March 2016

Model rules for date validation and triming the values before validation

Today I faced problem with validating date fields .

I am accepting date using CJuiDateTimepicker it is working fine, but when I gave regular expression to validate a date field. It is giving error even for the valid date.

The main cause for the above situation is the DatePicker picking up date and a space appended to the date at the end of date. However we have 'date' datatype in mysql for these fields so the dates are automatically trimmed and saved in DB perfectly.

But to validate the dates we have define the rules as below

array('heardt,fdate,secdt,judgedt,nheardt,mandsurdt,leactdt,serorddt,dispdt', 'filter', 'filter'=>'trim'),    // This will trim the values
            array('heardt,fdate,secdt,judgedt,nheardt,mandsurdt,leactdt,serorddt,dispdt','match' ,'pattern'=> '/^([0-9]{4})-([0-1]{0,1}[0-9]{1})-([0-3]{0,1}[0-9]{1})$/','allowEmpty'=>true,'message'=> 'Date Format is YYYY-MM-DD'),
/* The above one validates the format of date. This format accepts 0000-00-00 as date */

array('dispdt','match','pattern'=> '/^([0-9]{4})-([0-1]{0,1}[0-9]{1})-([0-3]{0,1}[0-9]{1})$/','allowEmpty'=>false, 'message'=>'Disposed date mandatory when case is disposed','on'=>'disposed'),

/* The above one works when $model->scenario is assaigned to disposed. */

By this we can validate the dates using model rules

No comments:

Post a Comment