hamed hamed - 1 year ago 323
PHP Question

yii2 - model load function does not set some model attributes

I'm working on a PHP Yii2 application. I have a strange problem with yii2

function. Here is my problem:

I have a form model called
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]) ?>

And this is my controller action:

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

return $this->render('increase-balance', [
'model' => $modelForm,

After submitting the form, I logged
and all three parameters
exist in the post with their right values(every thing is right). But after calling
function, this is my model attributes:

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

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

Answer Source

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

Check the rules() method to confirm it.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download