Skip to content

[LiveComponent] breaking without data-model declared #704

Closed
@robert-hoffmann

Description

@robert-hoffmann

Look like i broke the system again ^^

live_controller.js:258 Uncaught (in promise) Error: Cannot determine the model name for "<input type="checkbox" data-payload="{ &quot;courseId&quot;: 16, &quot;studentId&quot;: 50, &quot;year&quot;: 2023, &quot;week&quot;: 8 }">": the element must either have a "data-model" (or "name" attribute living inside a <form data-model="*">).

I have a list rendered with twig in a live component, this list has zero data-model dependencies (plain ole html)

However i spit out some checkboxes, and when i click the submit button, i fire off an api call to do some background work. This call gets all the selected checkboxes from the list and extracts metadata to populate the post request

<input
    type="checkbox"
    data-payload='{ "courseId": {{this.course.id}}, "studentId": {{student.id}}, "year": {{this.year}}, "week": {{this.week}} }'
    data-model="stupidPlaceHolder"
/>

However since i have a data-payload attribute, now live components expects a data-model attribute 😁

IMO, unless a reserved keyword, all attributes not matching those used by live components should be ignored.

Symfony is probably just detecting data-* with a regex

Would be nice to have this work diffrerently, or maybe a data-ignore attribute (at least like that i don't have to declare a placeholder in the component.php)

/**
 * INFO: only declared here because otherwise will break the component
 * and click behavior, if an element has a data-whatever, but no data-model
 */
#[LiveProp(writable: true)]
public ?string $stupidPlaceHolder = null;

Thanks 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugBug Fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions