Click to edit
A contact is shown as plain text. A click on Edit swaps in a form, and Save swaps the updated text back. The page does not reload and nothing outside the block changes.
Try it
Live demo
- Name
- Marina Duarte
- [email protected]
Real requests to this site. What you change is kept in a cookie of your own for an hour and is invisible to every other visitor.
Clear the name, or type an address with no dot in the domain, and press Save: the server answers 422 and the form
comes back with the messages. Fix it and the text returns.
The page
<div id="contact">
<dl>
<dt>Name</dt><dd>Marina Duarte</dd>
<dt>Email</dt><dd>[email protected]</dd>
</dl>
<button ypy-get="/contacts/1/edit" ypy-target="#contact" ypy-swap="outerHTML">Edit</button>
</div>What the server answers
GET /contacts/1/edit answers 200 with a form that has the same id as the block it replaces:
<form id="contact" ypy-put="/contacts/1" ypy-target="this" ypy-swap="outerHTML">
<input type="hidden" name="_token" value="…">
<label>Name <input name="name" value="Marina Duarte"></label>
<label>Email <input type="email" name="email" value="[email protected]"></label>
<button>Save</button>
<button type="button" ypy-get="/contacts/1" ypy-target="#contact" ypy-swap="outerHTML">Cancel</button>
</form>PUT /contacts/1 answers one of two things:
| Case | Status | Body |
|---|---|---|
| Valid | 200 |
The read-only block again, with the saved values |
| Name empty or email malformed | 422 |
The same form, with each field's message under it and aria-invalid="true" |
Every answer is a fragment, not a page, and carries Vary: YPY-Request so a cache never hands it to a visitor who
asked for the whole page.
How it works
-
ypy-geton the button asks for the form.ypy-target="#contact"names the block to replace andypy-swap="outerHTML"replaces the element itself. Withoutypy-swapthe default isinnerMorph, which keeps the block and morphs its children. -
The form carries
ypy-put. A form sends its fields onsubmit, so noypy-triggeris needed.ypy-target="this"points at the form, andouterHTMLswaps it for the read-only block. - The form has the same
idas the block, so the next click on Edit finds it again. - Cancel is
type="button", so it does not submit. It asks for the read-only block withypy-get. -
A
422is swapped like any other answer, so the form comes back with its errors. See Forms for the convention. _tokenis the CSRF field of a form that changes data; see CSRF.
Variations
- Use
ypy-patchinstead ofypy-putwhen the edit is a partial update. -
To put an error next to a field instead of redrawing the form, use
ypy-error-target. - Add
ypy-confirm="Discard changes?"to Cancel when the form is long.
See also
- Edit row, the same idea inside a table row
- Inline validation
- Attributes