5

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.

6
  • because field name not matching with model attribute name. Commented Nov 3, 2015 at 5:36
  • @IncognitoSkulll: Actually its very big form having more than 60+ input, dropdown, radio, checkbox etc.. fields. Commented Nov 3, 2015 at 5:53
  • @IncognitoSkulll: all client side validations are working except unique Commented Nov 3, 2015 at 5:53
  • don't use custom id for fields otherwise validation won't work. Commented Nov 3, 2015 at 5:55
  • 1
    No no .. I'm not using custom id bcoz it generates id dynamically Commented Nov 3, 2015 at 5:58

1 Answer 1

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.com/doc-2.0/guide-input-validation.html#client-side-validation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.