here are the model validation rules

public function rules() { return [ [['user_id', 'city_id', 'payment',], 'required'], ['street', 'required', 'message' => 'Укажите название улицы.'], ... ]; } 

that's how I create a form

 <div class="radio radio-tab"> <label><input id="delivery1" type="radio" name="Order[delivery]" checked="" value="0"> Самовывоз </label> </div> <div class="radio radio-tab"> <label> <input id="delivery2" type="radio" name="Order[delivery]" value="1"> Доставка </label> </div> <div class="tab-content"> <div class="tab-pane delivery1 active"> ... </div> <div class="tab-pane delivery2"> </div> ... </div> <?php ActiveForm::end(); ?> 

thus form ajax request

 $('.radio-tab input').change(function () { var id = $(this).attr('id'); if(id=='delivery1'){ ... }) if(id=='delivery2') $.ajax({ method: "POST", url: "/cart/delivery", data: true, success: function (data) { ... $('.radio-tabs .tab-pane.delivery2').html(data); $('.radio-tabs .tab-pane.delivery2').addClass('active'); } }) 

so I process it in the controller

 public function actionDelivery(){ $model=new Order(); return $this->renderAjax('_deliveryPiter',[ 'model'=>$model, ]); } 

and so I deduce

 ... <div class="col-xs-12 col-sm-4"> <div class="form-group field-order-street <?= $model->isAttributeRequired('street') ? 'required' : '' ?>"> <?= Html::activeLabel($model,'street'); ?> <?= Html::activeInput('text',$model,'street',['class'=>'form-control']) ?> </div> ... </div> 

and everything seems to be displayed but the question with validation on the client remains open. That is, if the input is not completed, an error message is not displayed, and an incorrectly completed form is sent to the server where, respectively, validation on the server does not pass. The question is how you can organize the e-mail on the client.

    1 answer 1

    Check the code of your form: in your example, <div class="help-block"></div> not visible, usually error messages get into it. in ActiveForm::begin([]) you can pass the parameters enableClientValidation , enableAjaxValidation

    If you passed enableAjaxValidation = true , then you will need to validate the model that came with the ajax request, see the example performAjaxValidation

     public function actionDelivery(){ $model=new Order(); if ($model->load(Yii::$app->request->post()) { $this->performAjaxValidation($model); } return $this->renderAjax('_deliveryPiter',[ 'model'=>$model, ]); } protected function performAjaxValidation($model) { if (Yii::$app->request->isAjax && !Yii::$app->request->isPjax) { if ($model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; echo json_encode(ActiveForm::validate($model)); Yii::$app->end(); } } }