BitstylesPhoenix.Component.Form.ui_input

You're seeing just the function ui_input, go back to BitstylesPhoenix.Component.Form module for more information.

Renders various types of <input> element, with the associated <label>s, and any errors for that field.

Attributes

  • type - The type of the input (see table below for available types). Defaults to type="text".
  • hidden_label - Only show the label for screen readers if set to true.
  • All options from above (see top level module doc).
  • All other attributes will be passed in as input options to the underlying input helpers from Phoenix.HTML.Form (see table below for used helpers). Defaults to maxlength="255" for email, text and password type. Set maxlength to false to prevent setting maxlength.

For reference which input helper is used check out the following mapping:

typeHelper
:checkboxPhoenix.HTML.Form.checkbox/3
:colorPhoenix.HTML.Form.color_input/3
:datePhoenix.HTML.Form.date_input/3
:datetime_localPhoenix.HTML.Form.datetime_local_input/3
:emailPhoenix.HTML.Form.email_input/3
:filePhoenix.HTML.Form.file_input/3
:numberPhoenix.HTML.Form.number_input/3
:passwordPhoenix.HTML.Form.password_input/3
:rangePhoenix.HTML.Form.range_input/3
:searchPhoenix.HTML.Form.search_input/3
:telephonePhoenix.HTML.Form.telephone_input/3
:textPhoenix.HTML.Form.text_input/3
:timePhoenix.HTML.Form.time_input/3
:urlPhoenix.HTML.Form.url_input/3

See the bitstyles form docs for examples of inputs, selects, textareas, labels etc. in use. See the bitstyles form docs for examples of form layouts.

Text field with label

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:name} />
...> """
"""
<label for="user_name">
  Name
</label>
<input id="user_name" maxlength="255" name="user[name]" type="text"/>
"""

Text required field with label

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:name} required/>
...> """
"""
<label for="user_name">
  Name
  <span aria-hidden="true" class="u-fg-warning u-margin-xxs-left">
    *
  </span>
</label>
<input id="user_name" maxlength="255" name="user[name]" required="required" type="text"/>
"""

Text field with error

iex> assigns=%{form: @form_with_errors}
...> render ~H"""
...> <.ui_input form={@form} field={:name} />
...> """
"""
<label for="user_name">
  Name
</label>
<input id="user_name" maxlength="255" name="user[name]" type="text"/>
<span class="u-fg-warning" phx-feedback-for="user[name]">
  is too short
</span>
"""

Text field with multiple errors

iex> assigns=%{form: @form_with_errors}
...> render ~H"""
...> <.ui_input form={@form} field={:email} />
...> """
"""
<label for="user_email">
  Email
</label>
<input id="user_email" maxlength="255" name="user[email]" type="text"/>
<ul class="u-padding-xl-left">
  <li>
    <span class="u-fg-warning" phx-feedback-for="user[email]">
      is invalid
    </span>
  </li>
  <li>
    <span class="u-fg-warning" phx-feedback-for="user[email]">
      must end with @bitcrowd.net
    </span>
  </li>
</ul>
"""

Text field with hidden label

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:name} hidden_label={true} />
...> """
"""
<label class="u-sr-only" for="user_name">
  Name
</label>
<input id="user_name" maxlength="255" name="user[name]" type="text"/>
"""

Text field with label (without maxlength)

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:name} maxlength={false}/>
...> """
"""
<label for="user_name">
  Name
</label>
<input id="user_name" name="user[name]" type="text"/>
"""

Text field with options

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input
...>   form={@form}
...>   field={:totp}
...>   label="Authentication code"
...>   label_opts={[class: "extra"]}
...>   placeholder="6-digit code"
...>   required={true}
...>   value=""
...>   inputmode="numeric"
...>   pattern="[0-9]*"
...>   autocomplete="one-time-code"
...>   maxlength={6} />
...> """
"""
<label class="extra" for="user_totp">
  Authentication code
  <span aria-hidden="true" class="u-fg-warning u-margin-xxs-left">
    *
  </span>
</label>
<input autocomplete="one-time-code" id="user_totp" inputmode="numeric" maxlength="6" name="user[totp]" pattern="[0-9]*" placeholder="6-digit code" required="required" type="text" value=""/>
"""

Email field with label

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:email} type={:email} />
...> """
"""
<label for="user_email">
  Email
</label>
<input id="user_email" maxlength="255" name="user[email]" type="email"/>
"""

Search field with placholder

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input
...>    form={@form}
...>    field={:email_or_name}
...>    type={:search}
...>    placeholder="Search by email or name"
...>    autofocus={true} />
...> """
"""
<label for="user_email_or_name">
  Email or name
</label>
<input autofocus="autofocus" id="user_email_or_name" name="user[email_or_name]" placeholder="Search by email or name" type="search"/>
"""

File field for pdfs

iex> assigns=%{form: @form}
...> render ~H"""
...> <Phoenix.Component.form :let={form} for={@form} multipart>
...>   <.ui_input form={form} field={:file} type={:file} accept="application/pdf" />
...> </Phoenix.Component.form>
...> """
"""
<form enctype="multipart/form-data">
  <label for="user_file">
    File
  </label>
  <input accept="application/pdf" id="user_file" name="user[file]" type="file"/>
</form>
"""

Checkbox

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:accept} type={:checkbox} />
...> """
"""
<label for="user_accept">
  <input name="user[accept]" type="hidden" value="false"/>
  <input id="user_accept" name="user[accept]" type="checkbox" value="true"/>
  Accept
</label>
"""

Checkbox required

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:accept} type={:checkbox} required/>
...> """
"""
<label for="user_accept">
  <input name="user[accept]" type="hidden" value="false"/>
  <input id="user_accept" name="user[accept]" required="required" type="checkbox" value="true"/>
  Accept
  <span aria-hidden="true" class="u-fg-warning u-margin-xxs-left">
    *
  </span>
</label>
"""

Checkbox with label class

iex> assigns=%{form: @form}
...> render ~H"""
...> <.ui_input form={@form} field={:accept} type={:checkbox} label_opts={[class: "extra"]}/>
...> """
"""
<label class="extra" for="user_accept">
  <input name="user[accept]" type="hidden" value="false"/>
  <input id="user_accept" name="user[accept]" type="checkbox" value="true"/>
  Accept
</label>
"""