Hello!
I have a problem when form have submit button with name attribute.
I need to push the button 2 times to submit a form.
Example:
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Apply', ['class' => 'btn btn-success', 'name' => 'submit', 'value' => 'apply']) ?>
I have same effect with one button:
<?= Html::submitButton('Apply', ['class' => 'btn btn-success', 'name' => 'submit', 'value' => 'apply']) ?>
If I remove the name attribute it works fine:
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Apply', ['class' => 'btn btn-success']) ?>
This bug was appeared after updating to 2.0.4. I think that it can be related to https://github.com/yiisoft/yii2/issues/6642 .
If you can't reproduce the issue I can give you more information about the case.
Same problem here.
After reading #6642 comments i fixed it in this way:
In \vendor\yiisoft\yii2\assets\yii.activeForm.js
if (!$hiddenButton.length) {
$('<input>').attr({
type: 'hidden',
name: $button.prop('name'),
value: $button.prop('value')
}).appendTo($form);
} else {
$hiddenButton.prop('value', $button.prop('value'));
}
changed to
if (!$hiddenButton.length) {
$('<input>').attr({
type: 'hidden',
name: $button.prop('name'),
value: $button.prop('value')
}).appendTo($form);
$button.click();
} else {
$hiddenButton.prop('value', $button.prop('value'));
}
looks like it works fine.
@FMRed , your fix is working for me too.
@FMRed it fired twice confirm dialog if submit button has 'data-confirm attribute'.
This problem happen when the name value is 'submit'
@genichyar this link explain the problem that cause behavior.
"The NUMBER ONE error is to have ANYTHING with ID or NAME "submit" in your form. Rename your form elements or you cannot call .submit() on the form"
@pana1990, your solution works! Thank you.
I also find info about it on https://api.jquery.com/submit/
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures.
@genichyar thanks for the link :)
@FMRed you solution worked, manually edited the form script.
@FMRed I need to add your change to yiiActiveForm.js to fix the issue. Anyway it is required to put a name to the submit button (I think to enter the if ($button.length && $button.attr('type') == 'submit' && $button.attr('name')) {). Without a name the fix doesn't work.
@genichyar I think this issue is not solved. Why is it closed? I am not using any name at all so the @pana1990 comment doesn't apply in my case.
My case consist in a form generated through ajax (embedded inside the index view of a model). In the update or create view everything is worked as expected. The 2 clicks required issue appears only in the form generated with ajax.
Sorry for "necroposting" but problem is still actual.
@cebe I want to send different values to server if different buttons pushed. The only way to do it is name the submit buttons as "submit", in other cases value isn't sent to request. And without this feature I shound use javascript and some hidden fields to determine correct button, it looks ugly. :(
And this feature works fine without framework and jQuery. So, does jQuery disable native HTML feature? And can you provide some workaround in the framework (for example like @FMRed suggests) or it breaks core functionality?