Yii Framework Forum: Date validation inside rules() method of Model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Date validation inside rules() method of Model Rate Topic: -----

#1 User is offline   chamalsl 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 13-November 09

Posted 23 November 2009 - 09:51 PM

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

#2 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

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

#3 User is offline   Roberto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 15-October 09

Posted 25 November 2009 - 08:06 AM

View PostSander, 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'),
	);
}

1

#4 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 25 November 2009 - 08:16 AM

View Postchamalsl, 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.



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.');
        }
    }
}

2

#5 User is offline   shailendra 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 29-December 09

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
0

#6 User is offline   Luis Lobo Borobia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 25-March 10
  • Location:Buenos Aires

Posted 28 April 2010 - 11:04 PM

here you have to use 'datetime' type, instead of 'date'
Posted Image
The Social Network for Artists
0

#7 User is offline   passion 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 18-January 12

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

#8 User is offline   sumon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 10-November 12

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'),

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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