Checking for 'type', how to set the validation rule

I'm having difficulty setting a validation rule that checks for type datetime

Here's my code



<?php


array('create_time, udpate_time', 'type'=>'datetime', 'allowEmpty'=>false),


?>


you can add one valid rule such as follow:

array('create_time, udpate_time', 'ifErrorFormat','on'=>'insert'),

and define one function follow:



	public function ifErrorFormat($attribute,$params){


		if(!$this->hasErrors()) { // we only want to authenticate when no input errors


do something validate date format


			if(error){


				$this->addError('account','erorr date format');


			}


		}


	}


sharehua, thank you for the reply, but this is not what I was looking for. I want to know how to implement the type validation rule, that needs to be set in rules() model function

Try

<?php


array('create_time, udpate_time', 'type','type'=>'datetime', 'allowEmpty'=>false),

Thanx Mike, exactly what I was looking for, but now my datetime fields evaluates to false form the following Cookbook example.

The fields in my DB is datetime fields, the rules specify datetime fields, and the Cookbook example make use of CDbExp​ression('NOW()'), should NOW() be something else maybe?

date('Y-m-d H:i:s', time())

Still validate to false after submitting form



    * Created On must be datetime.


    * Update Time must be datetime.


how you set time ,can you detail here?

I've implemented from the the cookbook example and then call another method in my models



<?php





 class BAutoTimestampbehavior extends CActiveRecordbehavior


 {


     /**


      * The field that stores the creation time


      */


     public $created='create_time';


     /**


      * The field that stores the creation time


      */


     public $modified='update_time';





     /**


      * @param <string> $on


      * @return <boolean>


      */


     public function beforeValidate($on)


     {


         if($this->Owner->isNewRecord)


            $this->Owner->{$this->created}=$this->Owner->{$this->modified}=new CDbExp​ression('NOW()');


         else


            $this->Owner->{$this->modified}=new CDbExp​ression('NOW()');


         return true;


     }


}


?>




<?php


    public function behaviors()


    {


        return array(


            'BAutoTimestampbehavior'=>array(


                'class'=>'application.components.BAutoTimestampbehavior',


                // You can optionally set the filed name options here


                // $created, $modified


            ),


        );


    }


?>


I think you need to specify the dateTimeFormat for the validator.

Mike, this is the format currently in my db



2009-05-19 11:26:10


which is

yyyy-MM-dd hh:mm:ss
right?

so I have added the datetimeFormat to the rule as follows



<?php


array('create_time, update_time', 'type', 'type'=>'datetime', 'datetimeFormat'=>'yyyy-MM-dd hh:mm:ss', 'allowEmpty'=>false),


?>


but the rule still evaluates to false

try this 'Y-m-d H:i:s'

Didn’t work  :(

Quote

Mike, this is the format currently in my db


2009-05-19 11:26:10


which is

yyyy-MM-dd hh:mm:ss
right?

rule still evaluates to false

Hmm. Your format string looks correct to me. CTypeValidator uses CDateTimeParser. I've done a little test and this works:

<?php


$d='2009-05-19 11:26:10';


$t=CDateTimeParser::parse($d, 'yyyy-MM-dd hh:mm:ss');


echo date('Y/d/m H:i',$t);

So something must be wrong with your attribute value. Did you double check that your attribute contains a valid date string? Try to echo it, before validation to find out what's going on.

I've echoed out $bin->create_time before it gets saved. I have to mention that the valued does not get set from a web form and is not specified as safeAttribute. I've also removed the rule, performed the insert and the datetime values inserted in the DB is as follows



158, 0, 0.00, 1, '2009-05-20 12:16:23', '2009-05-20 12:16:23', , 7


Before the save() method call the $bin->create_time is empty, which is expected, because it only gets set in the overridden beforeValidate method of the BAutoTimestampbehavior class, and I assume it gets called in the save() function somehow

                


<?php


echo 'before save = create_time=>'.$bin->create_time;


if($bin->save())


?>


Better double check that also. Maybe echo it out in the behavior method.



<?php


  echo $this->Owner->{$this->created}


?>





The result is 


NOW()


I understand this to be correct, because this is what the column value will be for the insert statement when the record get saved, as for the actual date value, I don’t know how and where I should echo to see it  ???

In this case i’d simply change the behavior to not set the attribute to NOW() but date(‘Y-m-d H:i:s’) instead.

Mike, that worked, thanx mate. Another issue came up. The date inserted is 1 hour later than the date on my pc. Pc date is correct though  :-\

I remember now, that I initially had this problem when working with unix timestamps, that's why I changed it. Then, the date fields in my DB used to be integers(11)

This is perhaps caused by difference of timezone settings between your mysql server and PHP ini.