php - Yii2: safe validator on condition -


i trying use validation rule in model, it's not working.

i mean remains safe if select other option.

[['dhanwantri_bill_number'], 'safe',      'when' => function($model) {         return $model->bill_type =='d';                      },     'whenclient' => "function (attribute, value) {                          return $('#opdtestbill-bill_type').val() == 'd';                      }" ], 

am doing wrong? there alternative solution achieve same.

thanks.

rule bill_type like

[['bill_type'], 'string', 'max' => 20], [['bill_type','test_name','date'], 'required'], 

edit safe attribute public properties per doc

$when - callable - php callable return value determines whether validator should applied. yii\validators\validator

$whenclient - string - javascript function name return value determines whether validator should applied on client side. yii\validators\validator

as 'safe' validator tells attribute may set massive assignment, approach not appropiate. says when use load() method attribute can value. , if not marked 'safe' doesn't prevent setting value e.g. $model->dhanwantri_bill_number = 'asdf'. not proper solution.

more precisely: 'safe' attribute not have effect when $model->validate() (which called $model->save()) gets called. used when $model->load() called. if source code of safevalidatior class see nothing happens validator. validator doesn't anything. marker (you may want compare e.g. requiredvalidator). , load() 'when' expression not used. can 'safe' doesn't work 'when'. safe validator may used when rule gets evaluated validateattribute() empty nothing happens in point in time.

besides whenclient in code doesn't make sense. should happen here?

i guess there several ways of realizing that. 1 idea let controller set attributes not load(), rather set them explicitely , check there if $model->dhanwantri_bill_number should set or not. or use load() , revert attribute after loading according $model->bill_type set.

or implement setter method dhanwantri_bill_number in model , choose there if attribute gets set or not. maybe scenario dependent.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -