php - Yii2: How to add validation rules to the model class dynamically? -


as per yii2 docs validation rules can applied either implicit properties (table fields) or userdefined properties.

is there way create validation rules properties dynamically added model class ?

for example:

let there model class named 'filter' , model class named 'category' - categories laptop, palmtop, desktop etc. may want use trackpad area 1 of filter laptop category , diagonal length 1 of filter palmtop category , on. in every case filter conditions vary according category choose , may want add model class filter dynamically , validation if load via post successful.

you can code rules()-function build array of validation rules depending on scenario , data input. not requirement fixed array. unfortunately doing way leave validation issues on frontend (should need that), dynamic rules there don't work well.

from comments gather biggest issue seems attributes not loaded model. because both load() , setattributes() fill attributes considered safe.

there 2 methods define attribute safe:

  • give validation rule (at least safe)
  • get in list of attributes returned safeattributes() (by overriding it)

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -