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

[LiveComponent] Allow form elements to live outside the form tag by using "form" attribute #2400

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ function getModelDirectiveFromElement(element, throwOnMissing = true) {
return dataModelDirectives[0];
}
if (element.getAttribute('name')) {
const formElement = element.closest('form');
const formId = element.getAttribute('form');
const formElement = formId ? document.getElementById(formId) : element.closest('form');
if (formElement && 'model' in formElement.dataset) {
const directives = parseDirectives(formElement.dataset.model || '*');
const directive = directives[0];
Expand Down
3 changes: 2 additions & 1 deletion src/LiveComponent/assets/src/dom_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ export function getModelDirectiveFromElement(element: HTMLElement, throwOnMissin
}

if (element.getAttribute('name')) {
const formElement = element.closest('form');
const formId = element.getAttribute('form');
const formElement = formId ? document.getElementById(formId) : element.closest('form');
Comment on lines +164 to +165
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check getElementById(formId) returns a form element

// require a <form data-model="*"> around elements in order to
// activate automatic "data binding" via the "name" attribute
if (formElement && 'model' in formElement.dataset) {
Expand Down
13 changes: 13 additions & 0 deletions src/LiveComponent/assets/test/dom_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ describe('getModelDirectiveFromInput', () => {
expect(directive?.action).toBe('user.firstName');
});

it('reads name attribute if element lives out of form and use "form" attribute', () => {
const container = document.createElement('div');
const formElement = htmlToElement('<form data-model="*" id="my-form"></form>');
const element = htmlToElement('<input name="user[firstName]" form="my-form">');
container.appendChild(element);
container.appendChild(formElement);
document.body.appendChild(container);

const directive = getModelDirectiveFromElement(element);
expect(directive).not.toBeNull();
expect(directive?.action).toBe('user.firstName');
});

it('does NOT reads name attribute if form[data-model] is NOT present', () => {
const formElement = htmlToElement('<form></form>');
const element = htmlToElement('<input name="user[firstName]">');
Expand Down