Call another controller action from submit button

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

http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html#$action-detail

not really helping me…

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]

])

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

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.