Skip to content

Pass label_opts[for: ID] when a custom ID is provided on ui_input #126

Description

@agatheblues

When passing an ui_input like this:

<.ui_input
    form={f}
    field={:types}
    type={:checkbox}
    name={"#{input_name(f, :types)}[]"}
    value={"awesome"}
    checked_value={"awesome"}
  />

Because of the checked value, the id attribute of the input will be autogenerated by Phoenix to be myform_types_awesome (see https://github.com/phoenixframework/phoenix_html/blob/d6c9d5369096540462d837e231f1a1eb5fb50042/lib/phoenix_html/form.ex#L1176). It suffixes the name with the checked_value.

But the label helper (https://github.com/phoenixframework/phoenix_html/blob/d6c9d5369096540462d837e231f1a1eb5fb50042/lib/phoenix_html/form.ex#L1823) doesn't know about the checked_value and will have for=myform_types or so.

As a result the input id and label's for do not match.

We can alleviate this by passing a custom id to ui_input but then it only works if you pass label_opts=[for: CUSTOM_ID] as well.

I was wondering if ui_input should by default pass the label_opts[for: CUSTOM_ID] automatically if an id was given to the ui_input?

Activity

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

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