Yii Framework Forum: Can some body show how to validate a date yyyy-mm-dd? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Can some body show how to validate a date yyyy-mm-dd? Rate Topic: -----

#1 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 20 September 2009 - 11:04 AM

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. :rolleyes:
0

#2 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 20 September 2009 - 11:15 AM

What about regex validator? #\d{4}-\d{2}-\d{2}#
0

#3 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 20 September 2009 - 10:07 PM

View Postpestaa, on 20 September 2009 - 11:15 AM, said:

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..
0

#4 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 21 September 2009 - 12:54 AM

My problem, forget to put the line model->validate() inside my controller ;D
Thanks guy!
0

#5 User is offline   sluderitz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 26-February 09
  • Location:Germany

Posted 21 September 2009 - 03:16 AM

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.
0

#6 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 21 September 2009 - 05:04 AM

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! :lol:
0

#7 User is offline   spk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 20-October 09

Posted 24 October 2009 - 02:36 PM

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;
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users