yii2无法将表单值保存到db $ model-> getErrors()表示fileds为空

时间:2017-08-21 11:05:26

标签: yii2

伙计们我试图将表单值提交给数据库,但它不会保存我尝试使用$ model-> getError方法来查看错误但是它说:

Array ( [name] => Array ( [0] => نام cannot be blank. ) [family] => Array ( [0] => فامیلی cannot be blank. ) [phone] => Array ( [0] => شماره تماس cannot be blank. ) [email] => Array ( [0] => ایمیل cannot be blank. ) [address] => Array ( [0] => آدرس cannot be blank. ) [brithday] => Array ( [0] => سال تولد cannot be blank. ) [age] => Array ( [0] => سن cannot be blank. ) [ability] => Array ( [0] => توانایی cannot be blank. ) [role] => Array ( [0] => نقش cannot be blank. ) )

正如你所看到的那样,我说我的所有字段都是安全的,但它们并不适合浏览器检查他们设置的网络! +更改模型类中的某些规则过滤器并没有帮助我+我使用$ model-> save(false)将在表中创建一个没有数据的empety行 任何人都可以告诉我这里有什么问题你可以看到网络日志:

Developers[name]:gholi
Developers[family]:gholizade
Developers[phone]:1234567890
Developers[email]:aa@aa.com
Developers[address]:aa ciry
Developers[brithday]:1396/05/02
Developers[age]:123
Developers[ability]:php
Developers[role]:develoer
Developers[join_date]:

这是我的控制器动作创建

   public function actionCreate()
    {

        $model = new Developers();

        if (Yii::$app->request->post()) {
            $model->join_date = date('Y-m-d h:m:s');
            if ($model->save()){
                echo 1;
            }else{
                print_r($model->getErrors());
            }
        } else {
            return $this->render('_form', [
                'model' => $model
            ]);
        }

    }

和我的表单页面

<div class="developer-form">

        <?php $form = ActiveForm::begin([
            'id' => $model->formName(),
            'enableAjaxValidation' => true,
            'validationUrl'=>\yii\helpers\Url::toRoute('admin/site-admin/validation')
        ]); ?>

        <?= $form->field($model, 'name')->textInput(['minlength'=>true]) ?>
        <?= $form->field($model, 'family')->textInput() ?>
        <?= $form->field($model, 'phone')->textInput() ?>
        <?= $form->field($model, 'email')->textInput() ?>
        <?= $form->field($model, 'address')->textInput() ?>
        <?= $form->field($model, 'brithday')->widget(jDate\DatePicker::className()) ?>
        <?= $form->field($model, 'age')->textInput() ?>
        <?= $form->field($model, 'ability')->textInput() ?>
        <?= $form->field($model, 'role')->textInput() ?>
        <?= $form->field($model, 'join_date')->textInput() ?>


        <div class="form-group">
            <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>

        <?php ActiveForm::end(); ?>
    </div>

这是我的模特课

class Developers extends ActiveRecord
{


    public static function tableName()
    {
        return 'developers';
    }


    public function rules()
    {
        return [
            [[
                'name',
                'family',
                'phone',
                'email',
                'address',
                'brithday',
                'age',
                'ability',
                'role',
            ], 'required'],
            [['join_date'], 'safe'],
            ['email', 'email'],
            [['phone'], 'integer', 'min' => 10],
            [['address'], 'string', 'max' => 100],
            [['name'], 'string', 'min' => 3],


        ];
    }

1 个答案:

答案 0 :(得分:3)

您没有将值加载到模型,将if更改为:

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