最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Yii2 Implement client side unique validation for input field - Stack Overflow

programmeradmin1浏览0评论

I've one field in my big form i.e.

<?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>

Following is my ActiveForm options configuration:

<?php
$form = ActiveForm::begin([
            //'id' => 'printerForm',                
            'enableClientValidation' => true,
            'options' => [
                'enctype' => 'multipart/form-data',
            ]
]);
?>

I want to implement client side unique validation for this. I'm using unique validator for it but its only working for server side validation.

public function rules() {
        return [
     [['name'], 'unique'],
]
...
other validations
...
};

Other validations working perfectly but unique client side validation is not working.

I've one field in my big form i.e.

<?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>

Following is my ActiveForm options configuration:

<?php
$form = ActiveForm::begin([
            //'id' => 'printerForm',                
            'enableClientValidation' => true,
            'options' => [
                'enctype' => 'multipart/form-data',
            ]
]);
?>

I want to implement client side unique validation for this. I'm using unique validator for it but its only working for server side validation.

public function rules() {
        return [
     [['name'], 'unique'],
]
...
other validations
...
};

Other validations working perfectly but unique client side validation is not working.

Share Improve this question edited Nov 3, 2015 at 5:43 J.K.A. asked Nov 3, 2015 at 4:49 J.K.A.J.K.A. 7,40425 gold badges99 silver badges164 bronze badges 6
  • because field name not matching with model attribute name. – Insane Skull Commented Nov 3, 2015 at 5:36
  • @IncognitoSkulll: Actually its very big form having more than 60+ input, dropdown, radio, checkbox etc.. fields. – J.K.A. Commented Nov 3, 2015 at 5:53
  • @IncognitoSkulll: all client side validations are working except unique – J.K.A. Commented Nov 3, 2015 at 5:53
  • don't use custom id for fields otherwise validation won't work. – Insane Skull Commented Nov 3, 2015 at 5:55
  • 1 No no .. I'm not using custom id bcoz it generates id dynamically – J.K.A. Commented Nov 3, 2015 at 5:58
 |  Show 1 more ment

1 Answer 1

Reset to default 6

Finally I did it myself by enabling AJAX validation for a single input field and by using isAjax so that the server can handle the AJAX validation requests.

Following is the code:

In view:

<?= $form->field($model, 'name',['enableAjaxValidation' => true, 'validateOnChange' => false])->textInput(['maxlength' => 255]) ?>

And in controller:

    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {

            $nm= $_POST['BusinessProcessProfile']['name'];
            $result = Model::find()->select(['name'])->where(['name' => "$nm"])->one();
            if ($result) {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return \yii\widgets\ActiveForm::validate($model, 'name');
            } else {
                return false;
    }
}

It automatically calls validations rules defined in the Model.

For more info please refer : http://www.yiiframework./doc-2.0/guide-input-validation.html#client-side-validation

发布评论

评论列表(0)

  1. 暂无评论