Skip to content

Conversation

@rmoorman
Copy link
Contributor

@rmoorman rmoorman commented Nov 15, 2024

Remove the default nil value for the as attribute in the generated simple_form core component.

This default interferes with the form name present in the form struct provided through the component's for attribute when used with :let=.

For example, consider the following code:

<.simple_form :let={f} for={@form}>
  <%= inspect(f.name) %>
  <.input field={f[:bar]} label="Bar" />
</.simple_form>

When rendered, f.name is nil, and the name attribute of the form field only contains bar, missing the proper field name prefix from the form struct:

<div class="mt-10 space-y-8 bg-white">
  nil
  <div data-phx-id="m9-phx-GAgAfRg6YU4XV6tC">
    <label for="bar" class="block text-sm font-semibold leading-6 text-zinc-800" data-phx-id="m10-phx-GAgAfRg6YU4XV6tC">
    Bar
    </label>
    <input type="text" name="bar" id="bar" class="mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 border-zinc-300 focus:border-zinc-400">
  </div>
</div>

This issue been reported over here.

Fixes #5901

Remove the default `nil` value for the `as` attribute in the generated
`simple_form` core component.

This default interferes with the form name present in the form struct
provided through the component's `for` attribute when used with `:let=`.

For example, consider the following code:

```
<.simple_form :let={f} for={@Form}>
  <%= inspect(f.name) %>
  <.input field={f[:bar]} label="Bar" />
</.simple_form>
```

When rendered, `f.name` is `nil`, and the `name` attribute of the form
field only contains `bar`, missing the proper field name prefix from
the form struct:

```
<div class="mt-10 space-y-8 bg-white">
  nil
  <div data-phx-id="m9-phx-GAgAfRg6YU4XV6tC">
    <label for="bar" class="block text-sm font-semibold leading-6 text-zinc-800" data-phx-id="m10-phx-GAgAfRg6YU4XV6tC">
    Bar
    </label>
    <input type="text" name="bar" id="bar" class="mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 border-zinc-300 focus:border-zinc-400">
  </div>
</div>
```

This issue been reported [over here](phoenixframework#5901).

Fixes phoenixframework#5901
@rmoorman rmoorman force-pushed the simple-form-default-for-as-overrides-name-from-form-struct branch from 11a3c4f to 19680de Compare November 15, 2024 01:49
@SteffenDE SteffenDE merged commit 28e6dcb into phoenixframework:main Nov 26, 2024
5 of 7 checks passed
@SteffenDE
Copy link
Contributor

Thank you! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

simple_form default for @as overrides form values

2 participants