gpt4 book ai didi

php - yii2 - 模型加载函数没有设置一些模型属性

转载 作者:可可西里 更新时间:2023-10-31 23:56:45 25 4
gpt4 key购买 nike

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

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

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

这是我的 View 文件的一部分:

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

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

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

这是我的 Controller 操作:

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

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

提交表单后,我登录了Yii::$app->request->post()die()和所有三个参数 amount , receiptNumber , description以正确的值(value)观存在于岗位上(一切都是正确的)。但是在调用$modelForm->load之后函数,这是我的模型属性:

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

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

最佳答案

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

检查 rules() 方法以确认。

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

关于php - yii2 - 模型加载函数没有设置一些模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39587538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com