Answer the question
In order to leave comments, you need to log in
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):
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;
array(2) {
["code"]=>
array(1) {
[0]=>
string(23) "Код cannot be blank."
}
["name"]=>
array(1) {
[0]=>
string(33) "Название cannot be blank."
}
}
Answer the question
In order to leave comments, you need to log in
/[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.
It works for you
$model = new Test();
$model->save(); //тут идет валидация, а Вы указали 2 параметра как обязательные, а они пустые
$model = new Test();
$model->code = 12;
$model->name = 'Иннокентий';
$model->save();
$model = new Test();
$model->load(Yii::$app->request->post());
$model->save();
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 questionAsk a Question
731 491 924 answers to any question