Friday, February 17, 2012

Make rule of date in model yiiframework

If you want to simple create a rule validation about comparing between date, you have to see CCompareValidator. To create this validator just simple to use this. From the documentation of rules in model you can see this :


compare : CCompareValidator, compares the specified attribute value with another value and validates if they are equal.
  1. allowEmpty, whether the attribute value can be null or empty.
  2. compareAttribute, the name of the attribute to be compared with.
  3. compareValue, the constant value to be compared with.
  4. operator, the operator for comparison.
  5. strict, whether the comparison is strict (both value and type must be the same.
And to use this just see the example below to rules() in your model..

                 array(
     'start_time_planned',
     'compare',
     'compareAttribute'=>'end_time_planned',
     'operator'=>'<=', 
     'allowEmpty'=>false , 
     'message'=>'{attribute} must be greater than "{compareValue}".'
   ),
the operator can be =,<,>. and you can have a custom message for error in "message" property

1 comment:

  1. nice post.. it helps me a lot because i was hunting around without caching any animal.. cool..

    ReplyDelete