0

モデルがあります。typeIdの値に応じて、いくつかのパラメータが必要になります

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

だから私はこのようにそれを解決しました:

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;
    }
}    

別の方法はありますか?

4

0 に答える 0