The <ChangesetForm>
component binds an instance of ember-changeset
to a <form>
tag. This binding is achieved through the component's {{yield}}
, which is a hash of form controls that interface with the underlying changeset object.
Here's a list of yielded form components,
The data model used to back the <ChangesetForm>
is just a simple JS object, and nested property paths are supported:
// Just a simple JS object
export const myFormModel = {
name: {
first: 'Ron',
last: 'Swanson'
},
email: '[email protected]',
hometown: 'Pawnee, IN',
preferences: ['Lagavulin', 'Steak', 'Bacon', 'No govt.']
};
Changeset validation is provided by ember-changeset-validations
, and is optional for <ChangesetForm>
components. This takes the form of another JS object literal mapping properties to validation funcitons:
// validations.js
import validateFormat from 'ember-changeset-validations/validators/format';
import validatePresence from 'ember-changeset-validations/validators/presence';
export const myValidations = {
'name.first': validatePresence(true),
'name.last': validatePresence(true),
email: validateFormat({ type: 'email' }),
hometown: validatePresence(true),
preferences: validatePresence(true)
};
For added flexibility, you may provide your own Changeset
object for the form. In this case you are responsible for also providing any validation you wish to apply to your Changeset
. The following is a brief example of that method:
// validators are optional!
const validations = {
myValue: validatePresence(true)
};
const validatorFn = lookupValidator(validations);
const myModel = {
myValue: ''
};
const myChangeset = new Changeset(myModel, validatorFn, validations);
In the template, instead of providing your @model
and @validations
objects, you only need to provide the @changeset
object,
{{demo/changeset-form/example1}}
This maps to the <InputText>
component.
{{demo/changeset-form/example2}}
This maps to the <InputTextArea>
component.
{{demo/changeset-form/example3}}
This maps to the <InputDate>
component.
{{demo/changeset-form/example4}}
This maps to the <InputSelect>
component.
{{demo/changeset-form/example5}}
These map to the <InputCheckbox>
and <InputCheckboxGroup>
components respectively.
{{demo/changeset-form/example6}}
These map to the <InputRadio>
and <InputRadioGroup>
components respectively.
{{demo/changeset-form/example7}}
The <ChangesetForm>
also yields submit and reset buttons that do just what you might think. The buttons are automatically disabled based on the form's validation state. When the form isPristine
or isInvalid
the submit button will be disabled. The reset button will be disabled only when the form isInvalid
. This may be overridden if alternative disabled states are desired. When triggered, each button will call their respective handler action, onSubmit
and onReset
.