Yii Framework Forum: "not required" Field in model not saved to database? - Yii Framework Forum

Jump to content

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

"not required" Field in model not saved to database? Rate Topic: -----

#1 User is offline   munemune 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 07-November 09

Posted 14 March 2010 - 08:13 AM

I have model that some its field is set "not required", let's say user model have 3 field (login,pass, and birth date).

Login and pass are required field for user model.
And i put all 3 field in form, if i fill all the field only (login) and (pass) filed that saved to database.
While (birth date) always NULL or in MySQL its always set "01-01-1970" regardless what i fill in that field.

Just wondering why yii not save data to database that not set as "required" in model rule?

And there is any solution? except set a default value in database and set (birth date) rule to "require" so if the field is empty, the database/yii will fill it with default value.

Thank You.
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 14 March 2010 - 09:01 AM

Even a required field should normally be saved to the database.

Can you post your controller action and the Model class?
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 14 March 2010 - 09:46 AM

View Postmunemune, on 14 March 2010 - 08:13 AM, said:

...
Just wondering why yii not save data to database that not set as "required" in model rule?
...


Declare a 'safe' rule (or it won't be massively assigned). BTW any rule will do, e.g 'length'.

See section "Safe Attributes in 1.1"
http://www.yiiframew...uide/form.model

/Tommy

This post has been edited by tri: 14 March 2010 - 09:56 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
2

#4 User is offline   munemune 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 07-November 09

Posted 14 March 2010 - 10:16 AM

View Posttri, on 14 March 2010 - 09:46 AM, said:

Declare a 'safe' rule (or it won't be massively assigned). BTW any rule will do, e.g 'length'.

See section "Safe Attributes in 1.1"
http://www.yiiframew...uide/form.model

/Tommy


'Safe Attributes' and 'massively assigned' i think i miss those 2 or 4 keyword.

Yes, it's work now.

Thanks for the reply.
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