Answer the question
In order to leave comments, you need to log in
How to display validation errors for multiple records selected by a checkbox?
Hello. There was a problem with error output for multiple records.
I have a GridView where I display a table with all users, I can select several users with a checkbox and change their status by pressing one button under a certain condition (validation is in progress here), if some selected users have not passed validation, then I don’t change anything and I want to display errors on top in this form:
user1: date is not correct
...
user2: no name the
whole problem is that I only get an error for one user, for the first all the others I stupidly don’t know how to show it.
...
тут получаю список всех пользователей $users выбранных чекбоксом
...
foreach ($users as $key => $user) {
$user->validate();
if ($user->hasErrors()) {
$result = [];
foreach ($model->getErrors() as $attribute => $errors) {
$id = $user->id;
$result[Html::getInputId($model, $attribute)] = $errors;
}
}
if ($model->save()) {
$saved = true;
}
}
if ($saved) {
Yii::$app->session->addFlash('success', 'Статусы были изменены');
} else {
return $this->asJson(['id' => $id, 'validation' => $result]);
}
Answer the question
In order to leave comments, you need to log in
You need to get all the models that you want to change at once.
Here you have an array of id-shnikov.
You can get everything at once
$usersId = Yii::$app->request->post('items', []);
$users = Users::findAll($usersId);
// для каждой отдельной модели передаёте её массив ошибок
if ($user->hasErrors()) {
$result = [];
$result[Html::getInputId($model, $attribute)] = $model->getErrors();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question