learax
(Learax0x)
1
Hello,
I have this submit button on a view of controller A, but I need action Create and Update from controller B.
how can I call the Create and Update of Controller B from the submit button of view/controller A ?
this is the submit button:
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
thanks for your support
Lea
You need to change the HTML property "action" on your form.
<form action="controller/b"></form>
umneeq liked you to the area in the docs that shows you how to change it in the ActiveForm. if you use ActiveForm it would be something like
$form = ActiveForm::begin([
'action' =>['/controller/b','id'=>$model->id]
])
learax
(Learax0x)
5
Hello, thanks , but I have a ternary condition here :
$model->isNewRecord ? 'Create' : 'Update'
so I need to tell the form to either use Create from controller B, or Update from controller B
I’m stuck and don’t really know how to achieve that .
thanks guys for your support.
Lea
jkofsky
(Jkofsky)
6
This depends on the model you pass into the view from controller A. You can pass as many $variables to a view as you want.
For example (not currently actual use.)
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
'modelB'=> new User::model();
]);
}
In the view file you have access to a $model and $modelB:
// This would show an "Update" button.
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update',
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
// This would show an "Create" button.
<?= Html::submitButton($modelB->isNewRecord ? 'Create' : 'Update',
['class' => $modelB->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
Also, you can name the variables anything you want.
'model' => $this->findModel($id),
could be
'modelA' => $this->findModel($id),
you just have to change the $model to $modelA in the view.
Hope this helps.