-1

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'.

Josep Vidal
  • 2,580
  • 2
  • 16
  • 27
adnen manssouri
  • 51
  • 2
  • 11

3 Answers3

1

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'));
    }

}
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
0

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.

Josep Vidal
  • 2,580
  • 2
  • 16
  • 27
  • In this case I'm not using the model but I just want to read the variable sent from the view – adnen manssouri Dec 16 '19 at 13:54
  • Then don't use `model->load()`. Use `$params = Yii::$app->request->post()` and inside $params you will have $params['username']. Or you could replace `if($model->load(...` with if(Yii::$app->request->post('username')){...}` – Josep Vidal Dec 16 '19 at 14:03
  • @adnenmanssouri change your `Html::a` for `Html::submitButton`. – Josep Vidal Dec 16 '19 at 14:34
  • @adnenmanssouri instead of load etc.. do `if(Yii::$app->request->isPost){ var_dump("Hei"); die() } ` – Josep Vidal Dec 16 '19 at 15:02
  • @adnenmanssouri then there you have the problem, inspect the html with F12 in chrome and check what url is inside form action. – Josep Vidal Dec 16 '19 at 15:09
  • I think the URL is true because the 'var_dump' in the controller works correctly and displays the false case – adnen manssouri Dec 16 '19 at 15:23
0

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

adnen manssouri
  • 51
  • 2
  • 11