yiisoft/yii2

ActiveRecord form with two submit buttons doesn't post name of submit button

Aperta

#17.808 aperta il 17 gen 2020

 (11 commenti) (0 reazioni) (0 assegnatari)PHP (6784 fork)batch import
JShelp wanted

Metriche repository

Star
 (14.302 stelle)
Metriche merge PR
 (Merge medio 2g 15h) (47 PR mergiate in 30 g)

Descrizione

I have an "ActiveRecord" model named "Events" and a view with two submit buttons like below (generated HTML code in browser):

<form id="starterWizard__form" class="starterWizard__form" action="/wizard/step1?id=19623" method="post"></form>
<!-- my form fields -->
<input type="submit" class="btn btn-primary" name="Events[submitbutton]" value="Save and Exit" form="starterWizard__form">
<input type="submit" class="btn btn-primary" name="Events[submitbutton]" value="Continue" form="starterWizard__form">

In my controller I want to implement different logic based on which submit button is used to post the form. But the problem is that I don’t see “submitbutton” in $_POST. If I test it in a plain php file without Yii then it works. I have also noticed the issue seems to be due to javascript that Yii runs in the page. If I comment out the following line of script in the page, then problem gets resolved.

jQuery('#starterWizard__form').yiiActiveForm([], []);

Note that my form doesn't use client-side validation ('enableClientValidation' => false) and I have tried to add submit buttons using both ActiveField and Yii Html::submitInput and neither works. Below is related part of my Yii view:

$form = ActiveForm::begin([
	'enableClientValidation' => false,
	'id' => 'starterWizard__form',
	'options' => [
		'class' => 'starterWizard__form'
	]
]);
ActiveForm::end();

$form->field($model, 'submitbutton', [
		'template' => '{input}',
		'inputOptions' => [
			'form' => 'starterWizard__form',
			'type' => 'submit',
			'value' => 'Save',
			'class' => 'btn btn-primary'
		]
	]);

Html::submitInput('Continue >>', [
	'class' => 'btn btn-primary',
	'form' => 'starterWizard__form',
	'name' => 'Events[submitbutton]',
	'value' => 'continue'
]);

Guida contributor