Corex.Form (Corex v0.1.0-alpha.13)
View SourceHelper functions to work with forms.
Corex form components such as Corex.Checkbox and Corex.Select with classic form and with Phoenix form using the Phoenix.Component.form/1 function.
Examples
Classic Form
In a classic form, you can use the name attribute to identify the checkbox.
The generated parameters will be as follows: /?terms=true
This works in Controller View and Live View
<form id="my-form">
<.checkbox name="terms" class="checkbox">
<:label>I accept the terms</:label>
</.checkbox>
<button type="submit">Submit</button>
</form>Phoenix Form
In a Phoenix form, you can use the field attribute to identify the checkbox.
The generated parameters will be as follows: /?terms=true
Required
You must use the Corex.Form.get_form_id/1 function to get the form id and pass it to the form component.
In a Controller
<.form :let={f} for={@changeset} id={get_form_id(@changeset)}>
<.checkbox field={f[:terms]} class="checkbox">
<:label>I accept the terms</:label>
</.checkbox>
<button type="submit">Submit</button>
</.form>In a Live View
Required
- You must use the
Corex.Form.get_form_id/1function to get the form id and pass it to the form component. - You must enable the controlled mode. This allows the Live View to be the source of truth and the component to be in sync accordingly
<.form for={@form} id={get_form_id(@form)}>
<.checkbox field={@form[:terms]} controlled class="checkbox">
<:label>I accept the terms</:label>
</.checkbox>
<button type="submit">Submit</button>
</.form>
Summary
Functions
Returns the form id.
Functions
@spec get_form_id(Phoenix.HTML.Form.t() | Ecto.Changeset.t()) :: binary()
Returns the form id.
Accepts either:
Examples
In a Controller
<.form :let={f} for={@changeset} id={get_form_id(@changeset)}>
<.checkbox field={f[:terms]} class="checkbox">
<:label>I accept the terms</:label>
</.checkbox>
<button type="submit">Submit</button>
</.form>In a Live View
<.form for={@form} id={get_form_id(@form)}>
<.checkbox field={@form[:terms]} controlled class="checkbox">
<:label>I accept the terms</:label>
</.checkbox>
<button type="submit">Submit</button>
</.form>