php - Yii2 控制器发布请求为 NULL
问题描述
谁能说出为什么数据库中的控制器帖子为 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]);
}
您可以查看是否验证错误并修复此问题
推荐阅读
- javascript - `react-apollo` `MockedProvider` 需要超时?
- ldap - .ldaprc、ldaprc 或 LDAP 无法识别的环境变量
- database - PostgreSQL join 显示重复
- ftp - 如何使用 FTP 在 android studio 中使用 Kotlin 上传小文件或图像
- python - Pandas - 在 for 循环中附加具有不同列的 DataFrame
- virtualbox - 如何在 Virtualbox (Windows 10) 中创建桥接连接
- c# - c#在foreach循环中增加int变量
- vba - 运行时错误“-2147352567 (80020009)”:数据已更改
- php - 有没有办法在一个 data.php 文件中进行多个查询?
- php - 无法测试引用模拟方法 PHPUnit 的方法