I have a form displayed inside a detailView
It is located in 'value'=>$form->field($model, 'dummy5'),
<?= DetailView::widget([
'model' => $model,
'options' => ['class' => 'detail1-galeria-view2'],
'attributes' => [
// cria um array com a fotografia, em que carrega a path no campo fieldName da bd
[
'attribute'=>'',
//'value'=>$model->foto,
'value'=>Html::a(Html::img(Yii::$app->getUrlManager()->getBaseUrl() . "/" .$model->foto, ['width'=>'192', 'height' => "256"]), $model->foto),
'format' => 'raw',
],
[
'attribute'=>'',
'value'=>$model->nome,
],
[
'attribute'=>'',
'value'=>$model->categoria,
],
[
'attribute'=>'',
'value'=>$model->descricao,
],
[
'attribute'=>'',
'value'=>$model->valor.' '.'€',
],
[
'attribute'=>'',
'format' => 'raw',
'value'=>$form->field($model, 'dummy5'),
],
// info
[
'attribute'=>'',
'format' => 'raw',
// nesta hiperligação passo o valor do model->nome deste registo para encomendas/create
//'value'=> Html::a(Yii::t('app','Comprar'), Url::toRoute(['encomendas/create', 'nome' => $model->nome, 'preco' => $model->valor])),
'value' => Html::a('Submit', ['encomendas/create', 'nome' => $model->nome, 'preco' => $model->valor, 'qt' => $model->dummy5], ['data' => ['method' => 'post', 'params' => ['action' => 'produtos/view2']]]),
],
],
]) ?>
I need to submit the form value to the controller using Html::a
or if not possible this way, trying with a submitButton that can pass all the other arguments ('nome' => $model->nome, 'preco' => $model->valor
).
Why do i prefer Html::a
to submit (if possible, instead of Html::submitButton
), because i learn how to pass values via get to the controller, and with Html::submitButton
i don't know if it can be done.
The downside of Html::a is that it doesn't validate my model, so nothing arrives to the controller in:
$qt = Yii::$app->request->get('qt');
Photos from both detailView with form and next view where i want $qt coming from the other form and to be inserted has default value in this new form:
Any ideas?
I think that your problem may be a mixture of POST
and GET
variables. Your Html::a
link is not a submit button, and won't collect data from the form. As your models attribute qt1
is empty for the model, no data is being submitted for the link.
To get around this you need to combine GET
and POST
requests, something like this;
'format' => 'raw',
'value' => Html::submit('Submit')
This will now pick up and submit via a post
request, and form data that the user has added.
You will need to change the action
parameter for the form. This is where you can create the GET
parameters for the submission;
Where you create your form, do it like this;
$form = ActiveForm::begin([
'action' => Url::to(['encomendas/create', 'nome' => $model->nome, 'preco' => $model->valor])
]);
And your filed should be
'attribute'=>'',
'format' => 'raw',
'value'=>$form->field($model, 'qt'),
],
Now, in your action encomadas/create
you will have access to the following variables:
Via the GET
request - nome
and preco
.
Via the POST
request - qt1
You can now use these as appropriate in your action and views.
Html::a('Link Name', ['url/to'], ['data' => [
'method' => 'post',
'params' => ['user_id' => Yii::$app->user->id],
], 'class' => 'btn btn-primary']);