Model And Form

I am not sure if this has been addressed or not.

In Yii 1 we basically used CActiveRecord to extend our models on.

Then used the rules as a way to validate our forms on, I couldn’t find any better way to do this but it makes our model very messy.

I didn’t know if there was a way in Yii2 where we could create a Form and then link it to our model.

An example of this would be,




class Form

...

public $model = 'Users';

public $fields = array('username','password');

...



What this would do is automatically allows me to create a form base on username and password.

or

If I want an additional password_repeat input then I can add it in the Form and not clutter up my model.

or

If I need to do different validation based on the given form displayed I can override that as well.

Let me know if there is anything like this.

Thanks

You can create a different model used for the form and validation and populate the database models attributes with the validated form values afterwards. You may also consider using http://www.yiiframework.com/doc-2.0/yii-base-dynamicmodel.html

I think having another model would work but imo is bad semantics.

DynamicModel is interesting I’ll have to look at that more, but still wouldn’t have the database link.

Hmm maybe I can work on a plugin to see if I could make this work, seems like it should be possible. I’ll post if I can conjure something up.

Thanks