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

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:

heex
<.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:

elixir
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.

elixir
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.

Add a Discard button

A discard block in the form's submit section draws a button beside Save while the form holds changes. It puts the form back to its starting values: an edit reads its record again, and a create starts over empty with the mount's defaults. A :tabs form stays on its tab. A confirm on the button asks first.

elixir
submit do
  discard label: "Discard", confirm: "Discard your changes?"
end

A page can do the same with MishkaGervaz.Form.Web.Live.discard/1, which takes the form's socket.

Draw the buttons in your page

Mount the form with submit: :external to hide its submit row for a create and for an edit. Your page then draws the buttons, and each carries form="<id>-form", the id of the form's <form>:

heex
<button type="submit" form="post-form-form">Save post</button>

The save event is still accepted. submit: false hides only the submit row of a create, because a record that can be changed and not saved is a trap.

Next steps