Post that comes:

[ 'UserInfo' => [ 'name' => 'ewewe' 'surname' => 'ewewew' 'dateBirth' => '1229-12-12' 'sex' => 'man' ] ] 

Model data should be written to [yii \ db \ BaseActiveRecord: _attributes]:

 app\models\db\UserInfo#1 ( [yii\db\BaseActiveRecord:_attributes] => [] [yii\db\BaseActiveRecord:_oldAttributes] => null [yii\db\BaseActiveRecord:_related] => [] [yii\db\BaseActiveRecord:_relationsDependencies] => [] [yii\base\Model:_errors] => null [yii\base\Model:_validators] => ArrayObject#2 ( 

Here returns true:

 $model->load(Yii::$app->request->post(), ''); 

I know that there is a problem with names, I pass it on with such a script:

 $scope.userRegSecondForm = function(){ var secForm = { name: $('input[name="name"]').val(), surname: $('input[name="surname"]').val(), dateBirth: $('input[name="dateBirth"]').val(), sex: $('select[name="sex"] option[selected]').val(), }; var userSecondFormData = { UserInfo : secForm }; console.log(userSecondFormData); return userSecondFormData; } 
  • Validation specified for attributes? - Sergii Chenakal
  • I do load before validation, and accordingly should be written to earlier too. + rules in the model, I temporarily commented on - Dmytro Rudakov
  • With this load: $ model-> load (Yii :: $ app-> request-> post (), ''); you need to specify the rules. Do something like this and it will work: public function rules () {return [[['name', 'surname', 'dateBirth', 'sex',], 'string'],]; } - Sergii Chenakal
  • rules added, in case of vskiy, does not write anything. As far as I know, they are needed for $ model-> validate (), but everything is fine with me - Dmytro Rudakov
  • Through the load method, only those attributes that the rules have are loaded - Sergii Chenakal

1 answer 1

You transfer data to the model, with an explicit indication that the required data is contained in the root _POST, and not in the nested array

  $model->load(Yii::$app->request->post(), ''); // 2ой аргумент load пустая строка, что говорит модели "Не ищи данные в массиве "UserInfo" 

However, you still send data in the UserInfo array, of course, the model does not even see it.

To work specifically your code - or remove the 2nd argument in the load

 $model->load(Yii::$app->request->post()); 

Or do not add data when sending to UserInfo array

 $scope.userRegSecondForm = function(){ var secForm = { name: $('input[name="name"]').val(), surname: $('input[name="surname"]').val(), dateBirth: $('input[name="dateBirth"]').val(), sex: $('select[name="sex"] option[selected]').val(), }; console.log(secForm); return secForm; } 
  • The fact is that yesterday I did it first thing (I removed the 2nd argument in the load), but then the result returned to me false, now. Before you answer, I tried again, and it worked, load returned true and the attributes were recorded. Strange of course, but it works, thanks. - Dmytro Rudakov