简体   繁体   中英

For What Inputs Should I Define Rules In Model Class?

Should I define rules for all of input elements in a model class? For example the model form has 2 input type element and a textarea. Can I just define rules for those two input type and not for textarea ?

I have done so but the text entered inside the textarea isn't save but if I put a rule for example for its length it will be saved.

If you don't define any rules for an attribute (or define an unsafe rule), that attribute will not be set during mass-assignment ( $model->attributes = $_POST['ModelClassName'] ). That seems like the most likely reason why your textarea value doesn't get saved. See Securing attribute assignments .


Yii documentation suggests only creating rules for the fields that take user input. However, I found it useful to specify rules for all attributes, both based on business logic and underlying database storage (although I do not like the fact that the same class has to worry about both of these aspects), and then adding unsafe rules to the fields where mass-assignment should not be allowed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM