A form component which wraps a native <form>
element and provides form state management abstractions.
ember-fit-form
provides flexible state management for
forms. We aim to support many data and validation libraries
for use within your application's forms. We're also built on
ember-concurrency, so you can easily
support promise-aware hooks to manage your applications form state.
Currently supported data models:
-
Validation Libraries:
-
Validation Libraries:
Please note that
ember-fit-form
does not provide form control components. It is simply an html form element with abstractions for state management.
ember install ember-fit-form
// my-form.js
rollback() {
return model.rollbackAttributes();
},
save() {
return model.save();
}
By default, ember-fit-form
expects Changeset models. To setup your default Model type, you should configure the component through config/environment
:
module.exports = function(environment) {
var ENV = {
emberFitForm: {
adapter: 'ember-changeset' // default
}
}
}
In the case that your forms use mixed Models throughout your application, you can overwrite the adapter
at the component level.
Coming Soon
- Actions
- Component Action Hooks
- Component Event Handler Hooks
- Attributes
isUnsubmittable
isSubmittable
isValid
isInvalid
isDirty
isPristine
isCancelling
isSubmitting
isValidating
didCancel
didSubmit
didValidate
Submits the form.
Submitting a form calls the form's validate
method and
then calls the form's onsubmit
hook if validation succeeds.
form.submit();
The
onsubmit
hook will never be called ifonvalidate
hooks is rejected.
Cancels the form.
Cancelling a form calls the form's oncancel
hook.
form.cancel();
Validates the form. Validating a form calls the validate action for each of the form's models.
form.validate();
The form
object is always curried in as the last argument for all
component action hooks.
The onsubmit
hook action is a promise-aware action which is called on form submission.
Form submission is triggered when calling form.submit()
.
save(/* form */) {
return model.save();
}
The
onsubmit
hook will not be called on form submission ifonvalidate
hooks is rejected.
The onsuccess
hook is a promise-aware action which is called when the onsubmit
hook is fulfilled.
success(/* result, form */) {
// Do something
}
The onerror
hook is a promise-aware action which is called when the onsubmit
hook is rejected.
error(/* error, form */) {
// Do something
}
The oncancel
hook is a promise-aware action which is called on form cancellation.
Form cancellation is triggered when calling form.cancel()
.
rollback(/* form */) {
return model.rollback();
}
The onvalidate
hook is a promise-aware action which is called on form validation.
Form validation is triggered when calling form.validate()
or form.submit()
On form submission, if onvalidate
returns a rejected Promise
or
false
, the submission will reject, and onsubmit
will not be called.
validate(/* form */) {
return model.validate();
}
The oninvalid
hook is a promise-aware action which is called when the onvalidate
hook is rejected or returns false
.
invalid(/* error, form */) {
// Do something
}
The form
object is always curried in as the last argument for all
component event handler hooks.
When onkeydown
is passed into fit-form
component, it registers the
keyDown
event on the html form element. The onkeydown
hook is called when
the keyDown
event is triggered.
handlekey(event, form) {
if (event.key === "Enter" && event.shiftKey) {
// Shift + Enter
form.submit();
} else if (event.key === "Escape") {
form.cancel();
}
}
When onkeyup
is passed into fit-form
component, it registers the
keyUp
event on the html form element. The onkeyup
hook is called when
the keyUp
event is triggered.
See onkeydown
example for usage.
When onkeypress
is passed into fit-form
component, it registers the
keyPress
event on the html form element. The onkeypress
hook is called when
the keyPress
event is triggered.
See onkeydown
example for usage.
Returns a Boolean value of the form's (un)submittability.
form.get('isUnsubmittable'); // true
You can still call
submit
ifisUnsubmittable
is true.
Returns a Boolean value of the form's submittability.
form.get('isSubmittable'); // true
Returns a Boolean value of the form's validity. A valid form is one where all of the form's models are valid.
form.get('isValid'); // true
Returns a Boolean value of the form's (in)validity. A invalid form is one where the some of the form's models are invalid.
form.get('isInvalid'); // true
Returns a Boolean value of the form's state. A dirty form is one with changes.
form.get('isDirty'); // true
Returns a Boolean value of the form's state. A pristine form is one with no changes.
form.get('isPristine'); // true
Returns a Boolean value of the form's cancelling state. A cancelling
form is one where the oncancel
hook is pending. This attribute is
commonly coupled with the cancel
action.
form.get('isCancelling'); // true
Returns a Boolean value of the form's submitting state. A submitting
form is one where the onsubmit
, onsuccess
, or onerror
hooks are
pending. This attribute is commonly coupled with the submit
action.
form.get('isSubmitting'); // true
Returns a Boolean value of the form's validating state. A validating form is one where the form's model(s) are validating upon form submission.
form.get('isValidating'); // true
Returns a Boolean value of the form's cancelled state. A cancelled form is one where the oncancel
hooks is settled.
form.get('didSubmit'); // true
Returns a Boolean value of the form's submitted state. A submitted form is one where the onsubmit
hooks is settled.
form.get('didSubmit'); // true
Returns a Boolean value of the form's validated state. A validated form is one where the form's model(s) were validated upon form submission.
form.get('didValidate'); // true