How to set a validation message for a field that has ajax validation in scripts? - Yii2

I use one model file for two forms. One for SIGNUP and the other for A dding members .

I have not installed any script for the SIGNUP form. But, the script for adding items is installed.

Model

public function rules() { return [ //Add Members ['first_name', 'required','message'=>'Please enter first name.','on'=>'addteammembersidebar'], ['email', 'required','message'=>'Please enter email address.','on'=>'addteammembersidebar'], ['mobile','required','message'=>'Please enter mobile number.','on'=>'addteammembersidebar'], //Common ['first_name', 'required','message'=>'Please enter your first name.'], ['email', 'required','message'=>'Please enter your email address.'], ['mobile','required','message'=>'Please enter your mobile number.'], ]; } 

View

Here I set the script as $modelTeamMembers->scenario = 'addteammembersidebar'; .

 <?php foreach ($modelsTeamMembers as $indexMember => $modelTeamMembers): $modelTeamMembers->scenario = 'addteammembersidebar'; ?> <tr class="house-item"> <td class="vcenter"> <?php // necessary for update action. if (! $modelTeamMembers->isNewRecord) { echo Html::activeHiddenInput($modelTeamMembers, "[{$indexMember}]id"); } ?> <?php $modelTeamMembers->first_name = $first_name; echo $form->field($modelTeamMembers, "[{$indexMember}]first_name")->label(false); ?> </td> <td> <?php $modelTeamMembers->last_name = $last_name; echo $form->field($modelTeamMembers, "[{$indexMember}]last_name")->label(false); ?> </td> <td> <?php $modelTeamMembers->email = $email; echo $form->field($modelTeamMembers, "[{$indexMember}]email",['enableAjaxValidation' => true])->label(false); ?> </td> <td> <?php $modelTeamMembers->mobile = $mobile_number; echo $form->field($modelTeamMembers, "[{$indexMember}]mobile", ['inputOptions' => ['class' => 'form-control', 'maxlength'=>"10"]])->label(false); ?> </td> </tr> <?php endforeach; ?> 

All validation error messages except the email field. If, I 'enableAjaxValidation' => true from the field, it works. But for me, 'enableAjaxValidation' => true is required.

Picture

enter image description here

As in the image, you can clearly see that the error message "Please enter your email address." Which should be "Please enter an email address." The email field authentication error message does not match. In addition, everything is in order.

How to set confirmation for email field for scripts? Any help / hint / suggestions are noticeable.

+5
source share
1 answer

Can I find out why you need to use email validation using AjaxValidation here? For this type, it is enough to write without it, since AjaxValidation more suitable when you want to search and retrieve data from a database or other models, rather than on your own.

However, if you think you need AjaxValidation , you should configure a few different things, as the current code does not work.


Configure AjaxValidation in Preview :

 // Set to: index.php?r=profile/email-validation $form = ActiveForm::begin(['validationUrl' => ['profile/email-validation']]); // This is set correctly (no changes are needed comparing to your attempt) echo $form->field($modelTeamMembers, "[{$indexMember}]email", ['enableAjaxValidation' => true])->label(false); 

Why is this needed? You have set AjaxValidation , but you have not set the URL that this Ajax will work on. It is installed in ActiveForm::begin() in most cases.


Configuring AjaxValidation in the Controller (required):

 // Method that renders your view file public function actionSomethingThatRendersView() { // Code here $user->scenario = 'addteammembersidebar'; // Custom scenario name return $this->render(/* the remaining code goes here */); } // This is the method that Ajax will send request to ($_POST[]) public function actionEmailValidation() { $post = Yii::$app->request->post(); if (!empty($post)) { Yii::$app->response->format = Response::FORMAT_JSON; // Must be in JSON format $profile = new User(); // Edit your class name in here // Custom scenario (must be the same as above otherwise you might get unexpected response) $profile->scenario = 'addteammembersidebar'; $profile->load($post); return ActiveForm::validate($profile); } } 

Why is this needed? Ajax will send the request, but the request will do nothing without any action. This will β€œcreate a new” object with the same rules and attributes and will try to validate the new dataset. For the rendering method, $obj->scenario must also be set, because otherwise it would use the default script.


There are no changes to the Model . Everything should remain the same as in your example.

If you want to create a unique email, you need to make changes to the Model :

 public function rules() { // ... ['email', 'unique', 'message' => 'Email must be unique'], // If your attribute is not in the same table as defined in class, then: ['email', 'unique', 'message' => 'Email must be unique', 'targetClass' => User2::className()], } 
+11
source

All Articles