2017-11-19 11 views
0

フォームのスイッチ値に従ってモデル内のルールを動的に置き換えたい。鑑みYii2 - モデル内で動的に設定されたルールを切り替える

<?php 
    $form = ActiveForm::begin([ 
     'enableAjaxValidation' => true, 
     'validationUrl' => Url::toRoute('anounce/validation') 
    ]); 
?> 

制御装置において:モデルから

public function actionValidation() 
{ 
    $model = new Anounce(); 
    if (Yii::$app->request->isAjax && $model->load(Yii::$app-> 
     request->post())) { 
     Yii::$app->response->format = 'json'; 
     return ActiveForm::validate($model); 
    } 
} 

抜粋:idanounce_type

class Anounce extends \yii\db\ActiveRecord 
{ 
    private $currentRuleSet; // Current validation set 
    // Here are arrays of rules with assignment 
    private $fullRuleSet; // = [...]; 
    private $shortRuleSet; // = [...]; 
    private $minRuleSet; // = [...]; 

    public function init() 
    { 
     parent::init(); 
     $this->currentRuleSet = $this->fullRuleSet; 
    } 

    public function rules() 
    { 
     return $this->currentRuleSet; 
    } 

    public function beforeValidate() 
    { 
     if ($this->idanounce_type === self::FULL) { 
      $this->currentRuleSet = $this->fullRuleSet; 
     } else if ($this->idanounce_type === self::SHORTER) { 
      $this->currentRuleSet = $this->shortRuleSet; 
     } else if ($this->idanounce_type === self::MINIMAL) { 
      $this->currentRuleSet = $this->minRuleSet; 
     } 
     return parent::beforeValidate(); 
    } 
} 

変数は、ルール間のスイッチです。 残念ながら、の値がcurrentRuleSetに割り当てられているにもかかわらず、フルルールセット(またはinitで使用されるルールセット)に従って検証が行われました。

ルールを動的に切り替える方法は?

答えて

1

ここで必要なのは、ユーザーの入力に応じて検証を変更することです。これを行うには、モデルでシナリオを定義します。

最初に、検証するフィールドを入力するシナリオを設定します。例username,password、およびemailのフィールドがあり、SCENARIO_FIRSTに2つのシナリオを定義した場合、usernamepasswordのみが検証されます。

public function actionValidation() 
{ 
    $model = new Anounce(); 

    //example 
    if($condition == true) 
    { 
     $model->scenario = Anounce::SCENARIO_FIRST; 
    } 

    if (Yii::$app->request->isAjax && $model->load(Yii::$app-> 
     request->post())) { 
     Yii::$app->response->format = 'json'; 
     return ActiveForm::validate($model); 
    } 
} 

ここでのシナリオについての詳細を読み、ここで検証してそれらを使用する方法:

http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios

+0

サウンド

public function scenarios() { return [ self::SCENARIO_FIRST => ['username', 'password'], self::SCENARIO_SECOND => ['username', 'email', 'password'], ]; } 

は、その後、あなたのコントローラでは、入力に応じてシナリオを設定します良い。あなたはシナリオに従ってルールを変更する(!)方法を知っていますか? 'PUBLICATION'のシナリオでは、[['upload']、 'file'、 'skipOnEmpty' => true、 'extensions' => 'png、jpg'] 'としてパブリケーションイメージを読み込み、 'PDF' '[' upload ']、' file '、' skipOnEmpty '=> true、' extensions '=>' pdf ']' –

+0

あなたは2つのルールを持ち、ONを使用できます。 true、 'extensions' => 'png、jpg'、 'on' => 'PUBLICATION'] 、次に他のルールの場合 [[アップロード]]、[ファイル]、[skipOnEmpty] 'extension' => 'pdf'、 'on' => 'PDF'] – mrateb

+0

素晴らしい!最後に、ユーザーが何かを変更したときだけでなく、データベースからモデルにロードされたデータ(初期設定のデフォルトシナリオを設定するとき)もルールの切り替えが必要でした。残念ながら、データをデータベースからモデルにロードしたばかりのメソッド/イベントは見つかりませんでした。たぶんあなたはそのような方法/イベントを知っていますか? –

関連する問題