yiisoft/yii2

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

Offen

#17.808 geöffnet am 17.01.2020

 (11 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)PHP (6.784 Forks)batch import
JShelp wanted

Repository-Metriken

Stars
 (14.302 Sterne)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

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'
]);

Contributor Guide