yiisoft/yii2

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

Open

#17,808 opened on Jan 17, 2020

 (11 comments) (0 reactions) (0 assignees)PHP (6,784 forks)batch import
JShelp wanted

Repository metrics

Stars
 (14,302 stars)
PR merge metrics
 (Avg merge 2d 15h) (47 merged PRs in 30d)

Description

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