Hi,
Can some body advice how to validate a date input, with format yyyy-mm-dd? I look through the doc, no built-in validator for this.
Thanks.  ![]()
Hi,
Can some body advice how to validate a date input, with format yyyy-mm-dd? I look through the doc, no built-in validator for this.
Thanks.  ![]()
What about regex validator? #\d{4}-\d{2}-\d{2}#
Thanks. But its not working…  ![]()
	public function rules()
	{
		return array(
			array('fromDate', 'required'),
			array('fromDate', 'match', 'pattern'=>'#\d{4}-\d{2}-\d{2}#'),
		);
	}
I try to enter a wrong date format, "2009-ab-12", it no show any error, simply insert into database, with value 0000-00-00…
I has call model->validate(), nothing happen. (This is a CFormModel)
I also tried
	public function rules()
	{
		return array(
			array('fromDate', 'required'),
			//array('fromDate', 'match', 'pattern'=>'#\d{4}-\d{2}-\d{2}#'),
			array('fromDate', 'validateDate'),
		);
	}
	public function validateDate() {
		if (preg_match('#\d{4}-\d{2}-\d{2}#', $this->fromDate)) {
			$this->addError('fromDate', 'Date Error');
		}
	}
Also no effect…
My problem, forget to put the line model->validate() inside my controller  ![]()
Thanks guy!
The regular expression is only a partial soluation, you still need to check if the date exists, i.e. 2009-02-30 should yield an error. PHP has the function checkdate(). So you could write your own validator that uses the regex check and checkdate(). I hope that Yii will have a date validator soon, though because of localization its not that easy to implement.
Just for sharing, I found another regexp to validate date after googling around:
array('fromDate', 'match', 'pattern'=>'%\b[0-9]{4}[/.-](?:1[0-2]|0[1-9])[/.-](?:3[01]|[12][0-9]|0[1-9])\b%'),
It checks the format and also the values, e.g,: 9876-54-32 is a valid format in my case, but not a valid date.
Thanks again guys!  ![]()
For validate date (for determine is exist this date) you could use this one (I use it in my projects)
	public function dateValidator($attribute,$params){
		if (!CTimestamp::isValidDate((int)$this->year, (int)$this->month, (int)$this->day)) {
			$this->addError($attribute,  'Selected data is invalid');
		}
		return true;
	}