Yii2控制器发布请求为NULL

问题描述 投票:0回答:2

谁能说出为什么数据库中的控制器发布为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() ?>
php yii2
2个回答
1
投票

ActiveRecord在模型validate()返回true时将数据插入db。如果模型属性验证发生此错误,并且validate()方法返回了错误的发布数据,则不会将其插入到db中以查看错误,您可以使用将控制器更改为此]

 $model = new Reg();   

    if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()){   
        return $this->redirect(['index']);   
    }   

    return $this->render('create', ['model' => $model]);   
}  

并且您可以查看是否验证错误并解决此问题


0
投票

$model->save(false);

© www.soinside.com 2019 - 2024. All rights reserved.