php - Yii2: custom validation for multiple attributes using OR not working -



php - Yii2: custom validation for multiple attributes using OR not working -

i trying write rule validates if attribute_a or attribute_b set;

one of next attributes must set : licitatii_publice or licitatiile_atribuite

the next code not work;

<?php namespace common\models; utilize yii\base\model; class abonamentvalidare extends model { public $licitatii_publice; public $licitatiile_atribuite; public $zone; public $judete; public $tari; public static $targetattribute = []; public function rules() { homecoming [ [['zone'], 'required'], [['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'], ['licitatii_publice', 'validate_tip_licitatie', 'targetattribute' => ['licitatii_publice', 'licitatiile_atribuite']], ]; } function validate_tip_licitatie($attribute, $param) { print_r($attribute); $this->adderror($attribute, 'eroarea'); } public function attributelabels() { homecoming array( 'licitatii_publice' => 'lp', 'licitatiile_atribite' => 'la', 'tari' => 'tari', 'judete' => 'judete', 'zone' => 'zone', ); } public function save() { homecoming false; } } ?>

well have done in case create validator this:

................ homecoming [ [['zone'], 'required'], [['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'], [['licitatii_publice, licitatiile_atribuite'], 'validate_tip_licitatie'], ]; ............ function validate_tip_licitatie($attribute, $param) { if(!$this->licitatii_publice && $this->licitatiile_atribuite) $this->adderror($attribute, 'eroarea'); }

in way show both fields error.

however have done in yii1, read yii2 should same. logic same.

if want show error 1 attribute can utilize

return [ [['zone'], 'required'], [['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'], [['licitatii_publice'], 'validate_tip_licitatie'], ];

what trying more fancy :), that. if want utilize targetattribute might have https://github.com/yiisoft/yii2/blob/master/framework/validators/existvalidator.php

just build own validator class.

well. after reading exist validator believe need. has examples on how utilize it.

php yii2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -