2016-12-14 88 views
0

我有地址,国家,地区的形式。如果地址不空,则需要国家和地区。如果地址是空的,那么国家,地区不需要。条件验证,如果输入字段为空或不是?

我定义表格模型的规则这样的方法使用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?? 
}], 

我的观点

... 
... 
.. 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') ?> 
... 
... 

如果我测试上面的代码,无论是地址在我的形式为空或不那么国家野外总是需要。

我做错了吗?当地址不为空,现在正在

修订

国家是必需的。

因此,如果我输入地址栏并按提交按钮,它会显示所需的错误,那么如果我在地址栏输入任何内容并按提交按钮,它将处理提交。

解决方案是

没有'enableAjaxValidation' => true,在开始形式

,这里是我的国家野外台模型规则

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

现在我有其他类似的问题,当我实现这个代码

[['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的参考。 –

相关问题