H
H
HellWalk2017-08-08 13:21:45
JavaScript
HellWalk, 2017-08-08 13:21:45

The data leaves, but the model swears that they are not there - why?

I’ll make a reservation right away, I’m just learning with Yii2, and there may be stupid mistakes.
I make a form for sending data, the data goes out (I check through Fiddler):
1f7edfb523715a3bbc88087112a2e468.png
But it gives an error for two fields: cannot be blank, as if there is no data (although the ajax validator checks the data normally):
In the controller I write:

$model = new Test();
$model->save();

echo '<pre>';
var_dump($model->getErrors());
echo '</pre>';
die;

and I get:
array(2) {
  ["code"]=>
  array(1) {
    [0]=>
    string(23) "Код cannot be blank."
  }
  ["name"]=>
  array(1) {
    [0]=>
    string(33) "Название cannot be blank."
  }
}

Code:
View
Controller
Model
Tell me, what's wrong? Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Proskurin, 2019-03-22
@Mambli-Joe

/[0-9]{4}/ - means that the string will contain 4 digits, not 4 digits.
Therefore, you need to add at the beginning ^ - the beginning of the line and at the end $ - the end of the line
/^[0-9]{4}$/ - the line must consist of numbers.

M
Maxim Timofeev, 2017-08-08
@HellWalk

It works for you

$model = new Test();
$model->save(); //тут идет валидация, а Вы указали 2 параметра как обязательные, а они пустые

And there are empty values. This is what validation errors indicate. Do this and there will be no errors:
$model = new Test();
$model->code = 12;
$model->name = 'Иннокентий';
$model->save();

or
$model = new Test();
$model->load(Yii::$app->request->post());
$model->save();

D
davidnum95, 2017-08-08
@davidnum95

You have the form in $_POST['Test'], and you simply load it into the model $_POST. Need like this:

$model->load(Yii::$app->request->bodyParams['Test'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question