0

I use Yii2 to send a form with one model using this submit button from the view to its controller:

<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

Also need to send a boolean variable to the controller when the button is pressed but I don't know how to include it outside of the model.

I can resolve it defining a Html::hiddenInput() but it will include the new hidden field into $model.

Roby Sottini
  • 2,117
  • 6
  • 48
  • 88
  • I don't understand exactly what you need.. A bool value when the button is pressed? You can pass a value from the button, read more here https://stackoverflow.com/questions/38496710/check-a-specific-submit-button-value-in-controller-yii2 – Jørgen Nov 30 '18 at 16:34
  • not clear what you are asking, can you describe the actual scenario for which you need to pass this `boolean` value. and including a hidden input inside the form won't require to add or define anything in the model unless its an `activeHiddenInput()` – Muhammad Omer Aslam Nov 30 '18 at 19:25

1 Answers1

1

In what I understand you need to know when user pressed the submit button, so you must set the name attribute for submit button. You can set that attribute in options. Example for your question

Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary','name'=>'submit-btn','value'=>($model->isNewRecord? 'create' : 'update')])

for more info read this link submitButton or submitInput.

For handle request in controller use Yii::$app->request->post():

Example:

$post = Yii::$app->request->post();
if(isset($post['submit-btn']) && ($post['submit-btn'] == 'create')){
       /** Your Code */ 
}
ttrasn
  • 4,322
  • 4
  • 26
  • 43