{{bind}}
Two-way binding between a form control and a field: the control shows the field's value, and what the user enters is written back. It works with useForm, useHttp, or any object with that property.
import Component from '@glimmer/component';
import { bind, useForm } from 'inertia-ember';
export default class Contact extends Component {
form = useForm({ email: '', message: '' });
<template>
<input type='email' {{bind this.form 'email'}} />
<textarea {{bind this.form 'message'}}></textarea>
</template>
}Arguments
{{bind form field validate=…}} goes on an <input>, <select> or <textarea>.
| Argument | Type | Default | Description |
|---|---|---|---|
form | object | Positional. Where the value lives: a useForm or useHttp form, or any object. | |
field | string | Positional. The field's name, or a dot path into nested values with a form: 'address.city', 'items.0.qty'. | |
validate | boolean | string | none | Named. With a Precognition form, validate the field: true on change (for text, when it loses focus), or the name of another event, such as 'input' or 'blur'. |
When the control has no name, {{bind}} sets it to field.
How each control is read and written
The value is written to the control when it's inserted and whenever the field changes (a reset(), a set() from code). It's read back on input for text controls and on change for the rest.
| Control | Event | The field gets |
|---|---|---|
Text, email, search, <textarea>… | input | The value, a string. null or undefined shows as empty. |
type='number', type='range' | input | A number, or '' when the input is empty. |
| Checkbox, when the field isn't an array | change | true or false. |
| Checkbox, when the field is an array | change | The array with this checkbox's value added or removed. |
| Radio | change | The checked radio's value, a string. A radio is checked when String(field) equals its value. |
<select> | change | The selected option's value. |
<select multiple> | change | An array of the selected values. |
type='file' | change | A File or null; with multiple, an array of Files. Setting the field to null clears the input. |
What it writes to
If the object has a set method (useForm and useHttp do), {{bind}} calls set(field, value), so dot paths and history work. Otherwise it assigns object[field] = value, which doesn't follow dot paths and only updates the template if that property is tracked.
Examples
Text fields and selects
import Component from '@glimmer/component';
import { bind, useForm } from 'inertia-ember';
export default class Shipping extends Component {
form = useForm({ name: '', country: 'MX', notes: '' });
<template>
<input {{bind this.form 'name'}} />
<select {{bind this.form 'country'}}>
<option value='MX'>Mexico</option>
<option value='US'>United States</option>
</select>
<textarea {{bind this.form 'notes'}}></textarea>
</template>
}Checkboxes and radios
A checkbox is a boolean, unless its field holds an array: then each checkbox adds or removes its own value.
import Component from '@glimmer/component';
import { bind, useForm } from 'inertia-ember';
export default class Preferences extends Component {
form = useForm({ newsletter: false, topics: [] as string[], plan: 'free' });
<template>
<label><input type='checkbox' {{bind this.form 'newsletter'}} /> Send me the newsletter</label>
<label><input type='checkbox' value='ember' {{bind this.form 'topics'}} /> Ember</label>
<label><input type='checkbox' value='inertia' {{bind this.form 'topics'}} /> Inertia</label>
<label><input type='radio' name='plan' value='free' {{bind this.form 'plan'}} /> Free</label>
<label><input type='radio' name='plan' value='pro' {{bind this.form 'plan'}} /> Pro</label>
</template>
}Radio and select values are strings. If the server expects a number, convert it in the form's transform.
Nested fields
With a form, the field can be a dot path. The control's name becomes the path too, unless you set one.
import Component from '@glimmer/component';
import { bind, useForm } from 'inertia-ember';
export default class Address extends Component {
form = useForm({ address: { street: '', city: '' } });
// Errors for nested fields usually come keyed by the flat path, `address.city`.
errorFor = (path: string) => (this.form.errors as Record<string, string | undefined>)[path];
<template>
<input {{bind this.form 'address.street'}} />
<input {{bind this.form 'address.city'}} />
{{this.errorFor 'address.city'}}
</template>
}The server decides how errors for nested fields are keyed. Most send the flat path (address.city), which a template can't reach as errors.address.city, nor with Ember's get helper, which follows the dots; a small function like errorFor above reads it.
Files
import Component from '@glimmer/component';
import { bind, useForm } from 'inertia-ember';
export default class Attachments extends Component {
form = useForm<{ cover: File | null; gallery: File[] }>({ cover: null, gallery: [] });
<template>
<input type='file' accept='image/*' {{bind this.form 'cover'}} />
<input type='file' accept='image/*' multiple {{bind this.form 'gallery'}} />
</template>
}Validate with Precognition
On a form made with a method and URL, validate calls form.validate(field) after the value is written. true validates on change; a string names the event.
import Component from '@glimmer/component';
import { bind, useForm } from 'inertia-ember';
export default class SignUp extends Component {
form = useForm('post', '/users', { username: '', email: '' });
<template>
<input {{bind this.form 'username' validate='input'}} />
{{! as the user types, debounced by the form's validation timeout }}
<input type='email' {{bind this.form 'email' validate=true}} />
{{! when the input loses focus }}
{{#if this.form.errors.email}}<p>{{this.form.errors.email}}</p>{{/if}}
</template>
}On an object without a validate method, validate does nothing.
Bind to your own tracked state
Without a form, {{bind}} assigns the property, so make it tracked.
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { bind } from 'inertia-ember';
class Filters {
@tracked query = '';
@tracked onlyOpen = false;
}
export default class OrderFilters extends Component {
filters = new Filters();
<template>
<input type='search' {{bind this.filters 'query'}} />
<label><input type='checkbox' {{bind this.filters 'onlyOpen'}} /> Only open</label>
<p>Showing {{if this.filters.onlyOpen 'open' 'all'}} orders matching “{{this.filters.query}}”.</p>
</template>
}Notes
- The React and Vue adapters bind with
value/onChangeandv-model;{{bind}}is this adapter's equivalent. {{bind}}doesn't validate in the browser. Native attributes such asrequiredstill work, and the server's errors land inform.errors.- With
<Form>there's nothing to bind: it reads the fields from the DOM byname.
Related: Forms, Precognition, useForm, useHttp.