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

Preserve original name when as is not provided to simple_form #5975

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
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
1 participant