2016-12-14 8 views
0

私は住所、国、地区のフィールドを含むフォームを持っています。住所が空でない場合は、国と地区が必要です。住所が空の場合、国、地区は必要ありません。入力フィールドが空であるかどうかの条件付き検証?

私は私のフォーム、その後の国のフィールドで空またはないアドレスが常に必要かどうか、上記のコードをテストした場合、私は私の見解

... 
... 
.. address field .. 
<?= $form->field($model, 'alamat')->textInput(['maxlength' => true,'class'=>'input_text'])->label('Address')?> 

... country field drop down .. 
<?= $form->field($model, 'negara_id')->dropDownList(ArrayHelper::map(TbNegara::find()->all(), 'negara_id', 'negara_name'),['prompt'=>'-- Pilih Negara --','id'=>'negara_id','class'=>'input_select'])->label('Country') ?> 
... 
... 

conditional validation as document says

public function rules() 
{ 
    return [ 
     [['username', 'password', 'email', 'tanggal_daftar','password_validate'], 'required'], 
     [['customer_id','propinsi_id', 'kota_id', 'kecamatan_id', 'user_status_id','poin'], 'integer'], 
     [['tanggal_daftar','mobile_token','fb_id','newsletter','agree','password_validate','old_password'], 'safe'], 
     [['username', 'jenis_kelamin'], 'string', 'max' => 10], 
     [['password'], 'string', 'max' => 70], 
     [['identitas'], 'required', 'on' => 'login'], 
     [['email','username'], 'unique','except' => 'login'], 
     [['email'],'email'], 
     [['nama', 'hobby', 'pekerjaan'], 'string', 'max' => 30], 

     //address field 
     [['alamat'], 'string', 'max' => 200], 

     //country field 
     [['negara_id'], 'string', 'max' => 3], 

     [['kode_pos'], 'string', 'max' => 6], 
     [['hp'], 'string', 'max' => 18], 
     [['is_agent'],'boolean'], 
     [['profile_picture'],'string','max' => 100], 
     [['password_validate'], 'compare', 'compareAttribute'=>'password', 'message'=>Yii::t('app', 'Password tidak sama')], 
     [['agree'],'compare','operator' => '==','compareValue' => true,'message' => ''], 

//country field  
[['country_id'], 'required', 'when' => function($model){ 
       return $model->address != null; 
       .. 
       what about the code if address is empty then country not required?? 
}], 

を使用して、このようなテーブルモデル規則の方法で定義

私は間違っていますか?アドレスがnullでないとき

UPDATED

国が今取り組んでいる必要があります。

アドレス欄に入力して送信ボタンを押すと、必要なエラーが表示されますので、アドレス欄に何も入力せずに送信ボタンを押すと、送信が処理されます。

ソリューションは、私はこのコード

を実装する際に、今、私は他の同様の問題を抱えている、ここで

とは、国フィールドのための私のテーブルモデルのルールは

[['negara_id'], 'required', 'when' => function($model){ 
       return $model->alamat != null; 
      }, 'whenClient' => "function (attribute, value){ 
       return $('#tbcustomer-alamat').val() != ''; 
      }"], 

あるフォームを開始'enableAjaxValidation' => true,ずに

です

[['propinsi_id'], 'required', 'when' => function($model){ 
       //district required if country is set 
       return $model->negara_id != null; 
      }, 'whenClient' => "function (attribute, value){ 
       return $('#negara_id').val() != ''; 
      }"], 

地区は必須ではありません国が設定されていても、それが必要です。私はアドレスと、国に入力し、空白地区を離れ、私が提出押すと

ので、それが提出処理しますが、地区内の結果は、サーバー側で私の検証作業などの[ 'propinsi_id' => [ 0 => 'ID Propinsi cannot be blank.' ] ]

何か空白になることが望めないが、クライアント側ではありません。国はここ

に設定されている場合は、AJAXを介してロードさ

地区は、私は、地区のフィールドクライアント側の検証のために間違って何をやっている地区フィールド

   <div class="form_grup leftside fl"> 
        <div class="input_select"> 
        <?php 
         if (isset($model->propinsi)){ 
          echo $form->field($model, 'propinsi_id')->widget(DepDrop::classname(), [ 
          'options'=>['id'=>'propinsi_id','class'=>'input_select'], 
          'data'=>[$model->propinsi_id=>$model->propinsi->propinsi_name], 
          'pluginOptions'=>[ 
           'initialize' => true, 
           //depends on country 
           'depends'=>['negara_id'], 
            'class'=>'input_select', 
           'placeholder'=>'-- Pilih Propinsi --', 
           'url'=>Url::to(['customer2/propinsi']) 
          ] 
         ]); 
         } else { 
          echo $form->field($model, 'propinsi_id')->widget(DepDrop::classname(), [ 
            'options'=>['id'=>'propinsi_id','class'=>'input_select'], 
            'pluginOptions'=>[ 
              'initialize' => true, 
              //depends on country 
              'depends'=>['negara_id'], 
              'class'=>'input_select', 
              'placeholder'=>'-- Pilih Propinsi --', 
              'url'=>Url::to(['customer2/propinsi']) 
            ] 
            ]); 
         } 
        ?> 
<!--      <i class="fa fa-angle-down"></i> --> 
        </div> 
       </div> 

ための図でありますか?

+0

は、すべてのモデルのルールを示しています。 –

+0

@InsaneSkull完了。 :D –

+0

だから、地区のフィールドの検証が必要かどうか?私には分かりません。国が設定されている場合は –

答えて

0

_form.php

<?php $form = ActiveForm::begin(['enableAjaxValidation' => true]); ?> 

コントローラ

if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) { 
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 
    return \yii\widgets\ActiveForm::validate($model); 
} 

モデル

[['country_id'], 'required', 'when' => function ($model) { return $model->address != null;}, 'enableClientValidation' => false ], 
[['propinsi_id'], 'required', 'when' => function ($model) { return $model->country_id != null;}, 'enableClientValidation' => false ], 
+0

これは働いてくれてありがとう、私は似たような問題があります。質問が更新されました。あなたに自由時間があることを願ってください。 –

+0

申し訳ありませんが、クライアント側の検証を無効にすることを意味しますか?問題はクライアント側の検証にあります。 –

+0

@DarkCyber​​。 idkが存在する可能性がありますが、今はclientValidationについての参照が見つかりません。 –

関連する問題