Yii2控制器发布请求为空



有人能说出数据库中控制器帖子为NULL的原因吗?但在vardump有数据

控制器

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

型号

public function rules()
{
return [
[['title', 'article', 'fio','country', 'position','tel', 'email','cert'], 'required',  'message'=>'required'],
[['title', 'article', 'fio','country', 'position','tel', 'email','cert'], 'string'],
[['title', 'article'], 'safe'],
];
}

查看

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?php echo $form->field($model,'title')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'article')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'fio')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'country')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'position')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'tel')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'email')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php echo $form->field($model,'cert')->textInput(['class'=>'header__enter__input','placeholder'=>''])->label(false) ?>
<?php ActiveForm::end() ?>
当模型validate((返回true时,ActiveRecord将数据插入数据库。如果此错误发生在模型属性验证和validate((方法返回错误后,数据将不会插入数据库以查看错误,则可以使用将控制器更改为此
$model = new Reg();   
if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()){   
return $this->redirect(['index']);   
}   
return $this->render('create', ['model' => $model]);   
}  

您可以查看是否验证中的错误并修复此

$model->save(false);

将通过跳过验证来强制保存您的数据,但这是一种糟糕的做法,

保存后使用$model->getErrors(),它将返回阻止数据从数据库存储的验证错误列表

最新更新