Yii Framework Forum: Yii Dynamical Validation. Is There Another Way? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Dynamical Validation. Is There Another Way? Rate Topic: -----

#1 User is offline   gv0zd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 06-April 12

Posted 13 February 2013 - 10:28 AM

I have some model. Depending on value of typeId some params should be required

    typeId=1	Required:		param2	param3	param4	param5	param6	param7		param9	
    typeId=2	Required:	param1		param3	param4	param5					
    typeId=3	Required:	param1		param3					param8	param9	
    typeId=4	Required:	param1		param3							
    typeId=5	Required:	param1									param10

So i solved it in this way:

    public function rules()
        return array(
            array('typeId, param1, param2', 'numerical', 'integerOnly'=>true),
            array('param3, param4, param5, param6, param7, param8, param9', 'length', 'max'=>20),
            array('param10, param11', 'length', 'max'=>255),
            array('created, deleted', 'safe'),
            array('typeId', 'required', 'on'=>'insert, update'),
            array('typeId', 'exist', 'attributeName'=>'id', 'className'=>'Type', 'message'=>'Select {attribute}'),
            array('id, typeId, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, created, deleted', 'safe', 'on'=>'search'),            
            //dynamic rules
            array('param1', 'required', 'on'=>'type2, type3, type4, type5'),
            array('param1', 'exist', 'attributeName'=>'id', 'className'=>'Param1', 'message'=>'Select {attribute}', 'on'=>'type2, type3, type4, type5'),
            array('param2', 'required', 'on'=>'type1'),
            array('param2', 'exist', 'attributeName'=>'id', 'className'=>'Param2', 'message'=>'Select {attribute}', 'on'=>'type1'),
            array('param3', 'required', 'on'=>'type1, type2, type3, type4'),
            array('param4', 'required', 'on'=>'type1, type2'),
            array('param5', 'required', 'on'=>'type1, type2'),
            array('param6', 'required', 'on'=>'type1'),
            array('param7', 'required', 'on'=>'type1'),
            array('param8', 'required', 'on'=>'type3'),
            array('param9', 'required', 'on'=>'type1, type3'),
            array('param10', 'required', 'on'=>'type5'),
    public function validate($attributes=null, $clearErrors=true)
            foreach($this->getValidators() as $validator)
            //validate dynamic rules
                    foreach($this->getValidators() as $validator)
            return !$this->hasErrors();
            return false;
    public function getSubscenario($typeId)
            case Signal::TYPE_1:
                return 'type1';
            case Signal::TYPE_2:
                return 'type2';
            case Signal::TYPE_3:
                return 'type3';
            case Signal::TYPE_4:
                return 'type4';
            case Signal::TYPE_5:
                return 'type5';
                return false;

Is there another way?

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users