H
H
hollanditkzn2017-06-15 12:28:06
Yii
hollanditkzn, 2017-06-15 12:28:06

Understanding the validation in the widget?

I use this widget https://github.com/unclead/yii2-multiple-input/wik... I have a limit in the line, there must be 50 characters. And if the user writes more than 50 characters in the input, for example 70 characters, then my page reloads and displays this entry on a white background )
But how to make it so that when he drove in the same page and went to this page or ajax checked the data for validity and immediately indicated this error
Here is the code in the controller

public function actionCreate()
    {
        $notification = $this->findNotification();
        $models = [new Custom()];
        $request = Yii::$app->getRequest();
        if ($request->isPost && $request->post('ajax') !== null){
            $data = Yii::$app->request->post('Custom', []);
            foreach (array_keys($data) as $index) {
                $model[$index] = new Custom();
            }
            Model::loadMultiple($models, Yii::$app->request->post());
            Yii::$app->response->format = Response::FORMAT_JSON;
            $result = ActiveForm::validateMultiple($models);
            return $result;
        }

        if(Model::loadMultiple($models, Yii::$app->request->post())) {
            foreach ($models as $custom) {
                if ($custom->save()){
                    return $this->redirect(Yii::$app->request->referrer);
                } else{
                   print_r($custom->getErrors());
                }
            }
        } else {
            return $this->render('create', [
               'models' => $models,
            ]);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-06-15
@webinar

use ajax validation:
reading on tostra
reading on yandex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question