Skip to content

[LiveComponent] null argument is filled as empty string #1691

Closed
@jannes-io

Description

@jannes-io

Strange DX when using nullable types in LiveAction.

Using the following LiveComponent:

#[AsLiveComponent('TestComponent', 'test_component.html.twig')]
class TestComponent
{
    use DefaultActionTrait;

    #[LiveAction]
    public function action(#[LiveArg] ?int $id): void
    {
        //...
    }
}

With the following template:

<div {{ attributes }}>
    <a
        data-action="live#action"
        data-live-action-param="action"
        data-live-id-param="{{ null }}"
    >
        Click me!
    </a>
</div>

Expected:
The function is executed with $id === null

Current:
TypeError: "string" cannot be assigned to "int or "null".

Could we maybe include some sort of type transformation that when a LiveArg is nullable, and the accepted types does not include string, and an empty string is passed in, the value is set to null?

Passing in the string "null" to the param, it does coerce it to null. Which just results in weird DX imo. Real world example:

<a
    data-action="live#action"
    data-live-action-param="setCreating"
    data-live-parent-id-param="{{ parentId is not null ? parentId : 'null' }}"
>

Could we take a look at this behavior and perhaps tweak it so we don't need to cast null to string "null" when using nullable types on LiveAction's LiveArgs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions