yii2 - 模型加载函数没有设置一些模型属性 [英] yii2 - model load function does not set some model attributes

查看:21
本文介绍了yii2 - 模型加载函数没有设置一些模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 PHP Yii2 应用程序.我对 yii2 yii\base\Model.load 函数有一个奇怪的问题.这是我的问题:

I'm working on a PHP Yii2 application. I have a strange problem with yii2 yii\base\Model.load function. Here is my problem:

我有一个名为 PaymentIncreaseBalanceForm 的表单模型,如下所示:

I have a form model called PaymentIncreaseBalanceForm like below:

class PaymentIncreaseBalanceForm extends yii\base\Model {
     public $amount;
     public $receiptNumber;
     public $description;
     ...
}

这是我的视图文件的一部分:

Here is part of my view file:

<?= $form->field($model, 'amount')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'receiptNumber')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>

这是我的控制器操作:

 public function actionIncreaseBalance()
 {
      $modelForm = new PaymentIncreaseBalanceForm();
      if ($modelForm->load(Yii::$app->request->post()))
      {
              //some logic
      }

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

提交表单后,我用 die() 和所有三个参数 记录了 Yii::$app->request->post()数量receiptNumberdescription 以正确的值存在于帖子中(一切都是正确的).但是在调用 $modelForm->load 函数后,这是我的模型属性:

After submitting the form, I logged Yii::$app->request->post() with die() and all three parameters amount, receiptNumber, description exist in the post with their right values(every thing is right). But after calling $modelForm->load function, this is my model attributes:

$amount => 1000,
$receiptNumber => 887412141,
$description => NULL,

$description 始终为 NULL!我不知道这个领域有什么问题.我的代码有问题吗?

$description always is NULL! I don't know what is the problem with this field. Is there any problem with my code?

推荐答案

可能没有为代码中的 description 属性添加规则.

Probably there is no rule added for description attribute in your code.

检查rules()方法来确认.

默认情况下,方法 load() 仅将安全"值应用于属性,如果当前场景中有规则,则该值被视为安全".

As default, method load() applies only "safe" values to attributes and value is considered "safe" if there is rule for it in the current scenario.

这篇关于yii2 - 模型加载函数没有设置一些模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆