Reputation: 8711
Can one validation rules be shared for Edit
and Create
pages?
For example, when creating a photo album, you must provide a name and select an image (the cover of that album). And then a form is serialized. Suppose a form looks like as,
<form>
<input type="text" name="name"/>
<br />
<input type="file" name="cover" />
</form>
Again : A field name
cannot be blank and an image should be selected in cover
But what about Edit
page?
Here name is a must, but cover
isn't (because user might leave it blank in order to to override an existing cover)!
Can this be done by default in Yii?
Upvotes: 0
Views: 86
Reputation: 6297
You can specify multiple scenarios for a rule :
array($attributes,$validation,'on'=>'update,insert')
http://www.yiiframework.com/wiki/266/understanding-scenarios
Upvotes: 0
Reputation: 807
You need to write the rule with scenario for creating, only for name:
array('name', 'required', 'on'=>'create'),
If your other fields (gender, country...) aren't related to any scenario then they will be applied also in 'create' scenario.
array('gender', 'required'),
Use $model->setScenario('create') on creating action.
Upvotes: 0
Reputation: 8830
If you want different validation for edit and Insert, You can set in model rules
as below
array('name', 'required', 'on'=>'create'),
Reference: http://www.yiiframework.com/wiki/266/understanding-scenarios
Upvotes: 2