Post inside controller not loading into model in Yii2

1.9k Views Asked by At

When I want to get the variable from the form the post action doesn't load .

This is my view:

<?php
        $form = ActiveForm::begin();
        ?>
            <div class="form-group">
                <input type="text" name="username" placeholder="FullName">

                <?= Html::a(Yii::t('app', 'Start'), ['start', 'link' => $model->link], ['type' => 'button','class' => 'btn btn-primary btn-round']) ?>
            </div>

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

This is my controller:

if ($model->load(Yii::$app->request->post())){
    exit(var_dump('everything is ok'));
}else {
    exit(var_dump('nothing is right'));

}

The result is 'nothing is right'.

3

There are 3 best solutions below

4
adnen manssouri On BEST ANSWER

Finally I find the solution

<?php
$form = ActiveForm::begin(
    [
        'action' => 'start',
    ]
);
?>
<div class="form-group">
    <input type="text" name="username" placeholder="FullName">
    <?= Html::a('submit', Url::to(['start', 'link' => $model->link]), ['data-method' => 'POST']) ?>
</div>

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

thank you for all

3
Muhammad Omer Aslam On

Apart from using the anchor link instead of a submit button, you are not using model to create active input hence the field names are without model names or the standard array format that Yii accepts, you should pass empty string to the load method as second parameter which is formName like below

$model->load(Yii::$app->request->post(),'');

So your complete form should look like

<?php
    $form = ActiveForm::begin(
        [
            'action' => 'start',
        ]
    );
?>
<div class="form-group">
    <input type="text" name="username" placeholder="FullName">
    <?php echo Html::submitButton(Yii::t('app', 'Start'), ['class' => 'btn btn-primary btn-round']) ?>
</div>

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

EDIT

and your controller code should look like below, mind the first check it needs to be there so that the code is run when you submit only not on page load

if (Yii::$app->request->isPost) { //this should be here before the rest of the code

    if ($model->load(Yii::$app->request->post(), '')) {
        exit(var_dump('everything is ok'));
    } else {
        exit(var_dump('nothing is right'));
    }

}
6
Josep Vidal On

This is because load() method looks for post data inside model name property and you are writing the input yourself instead of using the Yii method for forms.

So your post Yii::$app->request->post() returns:

array(
 'username' => 'value of username',
)

And your $model->load looks for

 array(
     'modelName' => array(
         'username' => 'value of username',
       )
  )

To make your post data too look like that you could do it the right way that is, delete your Input and use this method inside the form:

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

Or the wrong way, modify your input and inside username use:

<input type="text" name="modelName[username]" placeholder="FullName">

Of course where I put username you must put your real model name.