Disable Yii Validation Error Message on focus / key up - Yii2

喜你入骨 提交于 2019-12-11 04:25:00

问题


As default, error message coming on keyup and after pressing submit button in form (If any error exist for that particular attribute). Which Is Ok. Working perfectly fine.

But, Is it possible to disable error message on key up? Means, error message, if any, should come only on pressing submit button.

View

<?php $form = ActiveForm::begin([ 'id' => 'register-form']); ?>
    <?= $form->field($model, 'first_name',['inputOptions' => ['class' => 'form-control fname','placeholder'=>'First Name']])->label(false); ?>
    .
    .
<p><?= Html::submitButton('REGISTER', ['name' => 'register-button']) ?></p>     

Controller

public function actionRegister() 
{
  $model = new Users(); // User Model
  if ($model->load(Yii::$app->request->post())) {

    // For Ajax Email Exist Validation
    if(Yii::$app->request->isAjax ) {
      Yii::$app->response->format = Response::FORMAT_JSON;
      return ActiveForm::validate($model);
    }
        .
        .
    }
}

I found How to disable client side validation messages of active form in Yii 2?.

$form = ActiveForm::begin(['fieldConfig' => ['template' => '{label}{input}']]);

But, In this answer. Error message neither coming on key up nor on pressing submit button. To show error summary, I need to use <?= $form->errorSummary($model) ?>. So, Is there any way to disable key up error message and show error message as it was showing before only on pressing submit button.


回答1:


I found in .../widgets/ActiveForm.php. One option was there validateOnBlur which is by default set true. So, I changed it to false in my form.

@Gamitg & @Jithin: Thanks For Your Kind Support.

<?php $form = ActiveForm::begin([ 'id' => 'register-form','validateOnBlur' => false]); ?>

Now, It's Working fine.




回答2:


Add

'enableClientValidation'=>false

to your form options:

<?php $form = ActiveForm::begin([ 'id' => 'register-form','enableClientValidation'=>false]); ?>
    <?= $form->field($model, 'first_name',['inputOptions' => ['class' => 'form-control fname','placeholder'=>'First Name']])->label(false); ?>
    .
    .
<p><?= Html::submitButton('REGISTER', ['name' => 'register-button']) ?></p>


来源:https://stackoverflow.com/questions/37420018/disable-yii-validation-error-message-on-focus-key-up-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!