date:: allowEmpty

Hi Guys,

How to set rule for date to save to db if user is allow to blank the date textfield. I set datatype for date_start_operation as date in my sql db.


array('date_start_operation', 'type', 'type' => 'date', 'dateFormat' => 'yyyy-mm-dd', 'allowEmpty' => false),



it always throw error state that Date Start Operation must be date when i put blank inside.

when i put ‘allowEmpty’ => true), it will throw error

General error: 1292 Incorrect date value: ‘’ for column ‘date_start_operation’ at row 1

Please helps me…

How about this? Not tested yet though


array('date_start_operation', 'date', 'format' => 'yyyy-mm-dd', 'allowEmpty' => true),

Hi junxiong,

How about this? Not tested yet though


array('date_start_operation', 'date', 'format' => 'yyyy-mm-dd', 'allowEmpty' => true),

it will pop out same error say that

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1292 Incorrect date value: ‘’ for column ‘date_start_operation’ at row 1.

Anyone got try it before? How to set rule for date if user can put blank for date textfield. Thanks.

Same problem. Can’t figure it out. Please help!


array('received, air_date', 'date', 'format'=>'yyyy-MM-dd', 'allowEmpty'=>true)

When I submit form with filled [font=“Courier New”]‘received’[/font] field and empty [font=“Courier New”]‘air_date’[/font] field - I get error:


CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1292 Incorrect date value: '' for column 'air_date' at row 1

Is an empty date_start_operation allowed in the database?

Yes, it defaults to NULL.




...

`received` DATE NULL DEFAULT NULL,

`air_date` DATE NULL DEFAULT NULL,

...

try:




array( 'received, air_date', 'date', 'format'=>'yyyy-MM-dd', 'allowEmpty'=>true ),

array( 'received, air_date', 'default', 'setOnEmpty'=>true, 'value'=>null ),



Thanks, redguy, this works.

Why there’s no need to explicitly set null value for other NOT NULL columns, like e.g. text ones?

it depends on db engine. for text fields without setting attribute to null you will simply get table row with empty string instead of null (when displaying you won’t see the difference).

for date/datetime columns it does matter if you try to assign null or empty string. null is obvious, but when you try to assign any string (even empty one) to date column db engine must cast this string to date type. If database can cast empty string to date - everything will work (as I remember mysql does this and you will end up with date like 0000-00-00), if it cannot - it will throw error.

above applies to any other type: integers, floats, etc.

Thanks redguy, this solved my exact same problem.