Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit button with formaction outside of form tag #17805

Open
My6UoT9 opened this issue Jan 15, 2020 · 4 comments
Open

Submit button with formaction outside of form tag #17805

My6UoT9 opened this issue Jan 15, 2020 · 4 comments
Labels

Comments

@My6UoT9
Copy link
Contributor

My6UoT9 commented Jan 15, 2020

What steps will reproduce the problem?

Create a form, and put the submit button outside of the form.

And set the "form" attribute to the id of the form (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-form).

Now set the formaction attribute on the button.

What is the expected result?

On pressing that button, it should submit the form to my formaction.

What do you get instead?

It is submitted to the action of the form.

Additional info

It uses $form.on, and then filters by ":submit", since the submit button is not in the form, it is never going to set submitObject.
https://github.com/yiisoft/yii2/blob/master/framework/assets/yii.activeForm.js#L229

My current solution is:

$("[form=" + $form.attr("id") + "]:submit").on('mouseup.yiiActiveForm keyup.yiiActiveForm', function () {
    $form.data('yiiActiveForm').submitObject = $(this);
});
Q A
Yii version 2.0.32-dev
PHP version 7.4.1
Operating system Win 10
@My6UoT9 My6UoT9 changed the title Submit button outside of <form></form> and formaction Submit button outside of <form></form> and formaction Jan 15, 2020
@My6UoT9 My6UoT9 changed the title Submit button outside of <form></form> and formaction Submit button with formaction outside of form tag Jan 15, 2020
@samdark samdark added type:bug Bug status:ready for adoption Feel free to implement this issue. labels Jan 15, 2020
@yiisoft yiisoft locked and limited conversation to collaborators Jan 15, 2020
@yiisoft yiisoft unlocked this conversation Jan 15, 2020
@samdark
Copy link
Member

samdark commented Jan 15, 2020

Do you want to submit a pull request fixing it?

@My6UoT9
Copy link
Contributor Author

My6UoT9 commented Jan 15, 2020

I have a feeling my fix is suboptimal, I fear update through pjax might make problems.
Does the id of the form change on subsequent updates/validations?

@alex-code
Copy link
Contributor

Id should stay the same, to avoid conflicts when using Pjax you should ideally give your widgets their own unique ids instead of having auto generated ones.

Been a while since I've used jQuery but I think you'd need to include the current form so buttons within it are still picked up.

$("[form=" + $form.attr("id") + "]").add($form).on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () {
    $form.data('yiiActiveForm').submitObject = $(this);
});

@My6UoT9
Copy link
Contributor Author

My6UoT9 commented Jan 16, 2020

Thx @alex-code that snippet helped finalizing it; pr done.

@samdark samdark removed the status:ready for adoption Feel free to implement this issue. label Jan 16, 2020
My6UoT9 added a commit to My6UoT9/yii2 that referenced this issue Feb 19, 2020
form is actually more difficult to implement it seems, it is also related to yii.js, but it should at least work when data-form is used now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants