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: 43
  • 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)
    {
        if($clearErrors)
            $this->clearErrors();
        if($this->beforeValidate())
        {
            foreach($this->getValidators() as $validator)
                $validator->validate($this,$attributes);
    
            //validate dynamic rules
            if(!$this->hasErrors())
            {
                $scenario=$this->scenario;
                if($this->scenario=$this->getSubscenario($this->signalId))
                {
                    foreach($this->getValidators() as $validator)
                        $validator->validate($this,$attributes);
                }
                $this->scenario=$scenario;
            }
            $this->afterValidate();
            return !$this->hasErrors();
        }
        else
            return false;
    }
    
    public function getSubscenario($typeId)
    {
        switch($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';
            default:
                return false;
        }
    }


Is there another way?
0

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