Hi,
How can I do a date validation inside rules() method of model class(Check date is in correct format and day, month and year has correct values).
Is it possible to call a PHP method such as checkdate inside rules method.
Thanks,
Chamal.
Page 1 of 1
Date validation inside rules() method of Model
#2
Posted 25 November 2009 - 04:04 AM
This is not built in, but it can still be done pretty easily. You could use the 'match' validator which checks for a given regular expression, or you could create your own custom validator (just create a function in your model and specify the function name as validator in your rule). You can use checkdate in that custom function if you want.
#3
Posted 25 November 2009 - 08:06 AM
Sander, on 25 November 2009 - 04:04 AM, said:
This is not built in, but it can still be done pretty easily. You could use the 'match' validator which checks for a given regular expression, or you could create your own custom validator (just create a function in your model and specify the function name as validator in your rule). You can use checkdate in that custom function if you want.
In fact it is built in, you just have to use CTypeValidator with type "date" and set the dateFormat to the desired format. For example:
public function rules()
{
return array(
array('myDate', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),
);
}
#4
Posted 25 November 2009 - 08:16 AM
chamalsl, on 23 November 2009 - 09:51 PM, said:
Hi,
How can I do a date validation inside rules() method of model class(Check date is in correct format and day, month and year has correct values).
Is it possible to call a PHP method such as checkdate inside rules method.
Thanks,
Chamal.
How can I do a date validation inside rules() method of model class(Check date is in correct format and day, month and year has correct values).
Is it possible to call a PHP method such as checkdate inside rules method.
Thanks,
Chamal.
i guessing your mean is follow this:
public function rules()
{
return array(
array('dateField', 'myCheckdate',),
);
}
public function myCheckdate($attribute,$params)
{
if(!$this->hasErrors())
{
if(! strtotime($this->{$attribute}))
{
$this->addError($attribute,'The date is incorrect.');
}
}
}
#5
Posted 29 December 2009 - 07:02 AM
Can any one tell me how to add validation to date along with the time
There is some format like "yyyy-MM-dd" for date as in
public function rules()
{
return array(
array('myDate', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),
);
}
But wat format shud we follow to add time in this
There is some format like "yyyy-MM-dd" for date as in
public function rules()
{
return array(
array('myDate', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),
);
}
But wat format shud we follow to add time in this
#7
Posted 22 February 2012 - 04:24 AM
Hi all,
I am using CJuiDateTimePicker extension for my date field in form..
How to validate it using function rules()?
Thanks.
I am using CJuiDateTimePicker extension for my date field in form..
How to validate it using function rules()?
Thanks.
#8
Posted 29 November 2012 - 03:20 PM
Date validation Rules
array('date', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),
Share this topic:
Page 1 of 1

Help















