Place a form's groups in your page
v0.0.12
The Standard form template draws a form's groups one after another. A page that wants its own cards, steps or content between them can place each group itself, keep the form as one form, and work with it from outside: put a computed value into it, learn when it holds unsaved changes, and draw the buttons itself. This guide places the groups of a Gervaz form in a page, then covers those three tasks.
Before you begin
- Have a form with groups, and mount it on a page. See Build an admin form and Mount a table and a form in a LiveView.
- Use the
:standardlayout mode. A:wizardor:tabsform ignores the slot.
Place the groups with the body slot
Pass a :body slot to the form component. The slot receives the form, and MishkaGervaz.Form.Web.Live.group/1 draws one named group wherever you call it:
<.live_component
module={MishkaGervaz.Form.Web.Live}
id="post-form"
resource={MyApp.Post}
current_user={@current_user}
>
<:body :let={form}>
<section class="card">
<h2>Basics</h2>
<MishkaGervaz.Form.Web.Live.group form={form} name={:basics} />
</section>
<section class="card">The page's own content, not part of the form.</section>
<section class="card">
<h2>Publishing</h2>
<MishkaGervaz.Form.Web.Live.group form={form} name={:publishing} />
</section>
</:body>
</.live_component>The name is the name of a group of the form's groups section. A name the form has no group for raises an ArgumentError that lists the groups it has.
What stays the same
- It is still one form, one changeset and one save. A field's error shows in the card of the group that holds it.
- The header, the notices, the upload section, the form errors and the submit row stay where they are. The unsaved changes and Discard work as before.
- A group the page does not place is not drawn. A field in no group is drawn after the slot.
- While the form loads, the slot is drawn and each group shows that it loads. When the reader may not use the form, a group draws nothing.
Rules for the markup in the slot
Everything in the slot sits inside the form's one <form>. Put no <form> of your own there, and no input whose name is not the form's. An event of your own markup goes to the page unless it names a phx-target.
Put a value you worked out into the form
A page that works a value out while the form is open, such as the date a price preview was made for, puts it in without starting the form over:
MishkaGervaz.Form.Web.Live.put_values("plan-form", %{proration_date: preview.date})Each key names a field of the form, as an atom or a string. A name the form has no field for raises an ArgumentError. The form is validated with the values in it, as a typed change is, and the save sends them, a :hidden field's value included. What the reader typed into other fields stays, and the dirty state and the step stay as they were, because a value the page put is not a change the reader made. A field the reader may not change shows the value, and the save leaves it out. A form that is still reading its record, or being built again, takes the values once it is ready.
Learn when the form holds unsaved changes
Mount the form with notify_dirty={true}. It sends its page {:form_dirty, id, dirty?} each time it comes to hold changes and each time it holds none: after a save, a Discard, a Cancel or a start over.
def handle_info({:form_dirty, "post-form", dirty?}, socket),
do: {:noreply, assign(socket, :post_form_dirty?, dirty?)}The wrapper of the form, "<id>-form-wrapper", carries data-dirty="true" while the form holds changes.
Next steps
- Open a row in a drawer: put the form in a panel and ask before the panel closes.
- Form uploads, submit and presentation DSL: every option of the submit buttons.
- Form groups and layout DSL: the groups you place.