not exist class CDateValidator, but you can create it extending CValidator
and using like other validator
example
class CDateValidator extends CValidator {
protected function validateAttribute($object,$attribute)
{
if(!$this->isEmpty($object->$attribute,true)){
if (!is_date($object->$attribute)) { //is_date must be created
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is a bad date');
$this->addError($object,$attribute,$message);
}
}
}
}
in the model
public function rules()
{
return array(
array('date_field', 'CDateValidator'),
);
}
I had similar problems a week ago… so I will show you how I solved it.
First the validation of the date(s)
here is myDateValidate.php that I put in protected/components:
I use strtotime so that you can enter words representing date in a date field, like: now, yesterday, +1 week, …
<?php
/**
* myDateValidator - validates that the entered text is a valid date
*/
class myDateValidator extends CValidator
{
/**
* Validates entered text to be a valid date
*/
protected function validateAttribute($object,$attribute)
{
if(strtotime($object->{$attribute}))
{
$object->{$attribute}=date('d.m.Y',strtotime($object->{$attribute}));
}
else{
$this->addError($object,$attribute,'Wrong date!');
}
}
}
in the model you need to set the rules for the date fields like:
public function rules()
{
return array(
//.. any other rules ...
array('date_field1', 'myDateValidator'),
array('date_field2', 'myDateValidator'),
);
}
Now for the conversion of the dates to/from the database, In my case for the display I use mm.dd.yyyy, mysql and postgresql uses ISO 9075 yyyy-mm-dd
here is myDateFormat.php that I put in protected/components:
<?php
/**
* DateFormater - converts date from/to SQL database (ISO 9075=>"yyyy-mm-dd" to/from dd.mm.yyyy.
*/
class myDateFormat extends CActiveRecordBehavior
{
//.. array of columns that have dates to be converted
public $dateColumns = array();
/**
* Convert from $dateFormat to ISO 9075 dates before saving
*/
public function beforeSave($event)
{
foreach( $this->dateColumns as $date )
{
$_dt = $this->Owner->{$date};
if(ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})", $_dt, $regs))
$_dt = $regs[3]."-".$regs[2]."-".$regs[1];
$this->Owner->{$date} = $_dt;
}
return parent::beforeSave($event);
}
/**
* Converts ISO 9075 dates to dd.mm.yyyy after read from database
*/
public function afterFind($event)
{
foreach( $this->dateColumns as $date )
{
$_dt = $this->Owner->{$date};
if(ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $_dt, $regs))
$_dt = $regs[3].".".$regs[2].".".$regs[1];
$this->Owner->{$date} = $_dt;
}
return parent::afterFind($event);
}
}
in the model you define behaviors and set what fields are date fields, like:
public function behaviors(){
return array(
'myTimeStamp'=>array(
//.. this is a behavior that adds current timestamp to a field
'class'=>'application.components.myTimeStamp',
),
'myDateFormat'=>array(
'class'=>'application.components.myDateFormat', //.. says that myDateFormat is in protected/components
'dateColumns'=>array('date_field1','date_field2'), //.. says that date_field1 and date_field2 are field of type date, and need to be converted
),
);
}
So it seems that there is no specific need for a custom validator just to check a date.
What is concerning for me though, is that I can’t find any example of how to properly implement a “type” date validation rule, so this hint I’ve found is useless to me currently.
Has anyone else used the CTypeValidator for validating dates?
Looking at the code, you have it correct I believe my example just resulted in no errors being tossed, hadn’t tested it yet (as I’m working on other areas currently)