2015-08-28 89 views
0
/** 
    * @inheritdoc 
    */ 
    public function rules() { 
    return [ 
     [['quantity', 'first_name', 'last_name', 'email', 'country', 'postal_code', 'locality', 'address'], 'required'], 
     [['quantity'], 'integer'], 
     [['first_name', 'last_name', 'email', 'country', 'phone'], 'string', 'max' => 127], 
     [['postal_code'], 'string', 'max' => 20], 
     [['locality', 'address'], 'string', 'max' => 255] 
    ]; 
    } 

    public function scenarios() { 
     return [ 
      'firstStep' => ['quantity', 'first_name', 'last_name', 'email'], 
      'secondStep' => ['country', 'postal_code', 'locality', 'address', 'phone'], 
     ]; 
    } 

當我提交表單,我得到:Yii的場景 - 驗證無法觸發

無效參數 - 警予\基地\ InvalidParamException

未知場景:默認

有誰知道爲什麼?也許這不是覆蓋方案方法的正確方法。

+0

你只是檢查哪個字段是必需的或安全的,這一個定義.... – vishuB

+0

Vishu帕特爾:不明白你的意見對不起。請謹慎澄清? – MEM

回答

1
public function rules() { 
return [ 
    [['quantity', 'first_name', 'last_name', 'email', 'country', 'postal_code', 'locality', 'address'], 'required'], 
    [['quantity'], 'integer'], 
    [['first_name', 'last_name', 'email', 'country', 'phone'], 'string', 'max' => 127], 
    [['postal_code'], 'string', 'max' => 20], 
    [['locality', 'address'], 'string', 'max' => 255], 

    [['quantity', 'first_name', 'last_name', 'email'], 'required', 'on' => 'firstStep'], 
    [['country', 'postal_code', 'locality', 'address', 'phone'], 'required', 'on' => 'secondStep'], 
]; 
} 

you change last two line.... 

And now use scenario in your controller... 

$model->scenario = 'firstStep'; 

or, 

$model->scenario = 'secondStep'; 
+0

我相信如果我設置場景Vishu,我不必使用ON。 – MEM

+0

我得到的錯誤是由於默認方案(由Yii創建的未加載),因爲這不是覆蓋該方案的正確方法。 – MEM

+0

您在模型文件'[''quantity','first_name','last_name','email'],'required','on'=>'firstStep']中更改, [['country','postal_code' ,'locality','address','phone'],'required','on'=>'secondStep'],'我知道' – vishuB

0

確定。嚴格地說,我出現這個錯誤的原因是由於我沒有正確覆蓋場景方法,因爲像我這樣做,我不會保留被覆蓋的方法特定性。 鑑於此default error,我會在RETURN上完全替換。

爲了避免這種情況,我要好好做,因爲它的文檔指出:

即:

public function scenarios() { 

      $scenarios = parent::scenarios(); 

      $scenarios[self::SCENARIO_FIRST_STEP] = ['quantity', 'first_name', 'last_name', 'email']; 
      $scenarios[self::SCENARIO_SECOND_STEP] = ['country', 'postal_code', 'locality', 'address', 'phone']; 

      return $scenarios; 

    } 

當我們這樣做時,錯誤消失。

@Vishu Patel,但是,確實解決了我的隱含問題。所以我會接受它的答案。