2

As per the Yii2 docs validation rules can be applied either to the implicit properties (table fields) or to the userDefined properties.

Is there a way to create validation rules for the properties which are dynamically added to the model class ?

For example:

Let there be model class named 'Filter' and another model class named 'Category' - categories like laptop, palmtop, desktop etc. I may want to use trackpad area as one of the filter to the laptop category and diagonal length as one of the filter to palmtop category and so on. So in every case filter conditions vary according to the category I choose and I may want to add it to the model class Filter dynamically and a validation if the load via post was successful.

novice_developer
  • 131
  • 1
  • 2
  • 12
  • 3
    Nobody says the `rules()` function should be coded as a fixed array, it's perfectly possible to assemble it based on a number of conditions. It's only going to bite you if you want frontend validation as well, that's a lot harder. – Blizz Jun 20 '15 at 06:19
  • The problem is that model class fields are not getting populated if I don't declare my rules() for the dynamically added properties. i.e. the $model->load(yii->$app0->request->post()) is not loading the $model variables as load() is invoking the setAttributes() which is in turn invoking the validate() method and this fail as there are no rules declared for dynamically added properties. – novice_developer Jun 20 '15 at 07:04
  • Ah but that is not necessarily rules related. The `load` & `setAttributes` by default only accept `safe` attributes. You can simply override [`safeAttributes()`](http://www.yiiframework.com/doc-2.0/yii-base-model.html#safeAttributes()-detail) for that as well, independently of the validation. – Blizz Jun 20 '15 at 07:08
  • while trying to override the safeAttributes() I am returning the $this->attributes(), but it is returning only the table properties and not the user defined or dynamic properties. – novice_developer Jun 20 '15 at 07:36
  • Thanks for the help. I am able achieve this by overriding safeAttributes(). – novice_developer Jun 20 '15 at 16:38
  • I'll put all this down as an answer then so we can close the question – Blizz Jun 20 '15 at 16:42
  • Possible duplicate of [Yii2. Adding attribute and rule dinamically to model](http://stackoverflow.com/questions/38356701/yii2-adding-attribute-and-rule-dinamically-to-model) – devOp Jul 14 '16 at 07:11

1 Answers1

2

You can code the rules()-function to build an array of validation rules depending on the scenario and data input. It is not a requirement that this is a fixed array. Unfortunately doing it this way will leave you with validation issues on the frontend (should you need that), dynamic rules there don't work so well.

From the comments I gather that the biggest issue seems to be that the attributes are not loaded into the model. This is mainly because both load() and setAttributes() only fill attributes considered to be safe.

There are 2 methods to define an attribute as safe:

  • Give it a validation rule (at the very least safe)
  • Get it in the list of attributes returned by safeAttributes() (by overriding it)
Blizz
  • 8,082
  • 2
  • 33
  • 53