ember-fit-form
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.
Installation
ember install ember-fit-form
Usage
Example
// my-form.js
rollback() {
return model.rollbackAttributes();
},
save() {
return model.save();
}
Configure Adapter
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.
Custom Adapters
Coming Soon
API
- Actions
- Component Action Hooks
- Component Event Handler Hooks
- Attributes
isUnsubmittable
isSubmittable
isValid
isInvalid
isDirty
isPristine
isCancelling
isSubmitting
isValidating
didCancel
didSubmit
didValidate
Fit-Form Actions
submit
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.
cancel
Cancels the form.
Cancelling a form calls the form's oncancel
hook.
form.cancel();
validate
Validates the form. Validating a form calls the validate action for each of the form's models.
form.validate();
Fit-Form Component Action Hooks
The form
object is always curried in as the last argument for all
component action hooks.
onsubmit
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.
onsuccess
The onsuccess
hook is a promise-aware action which is called when the onsubmit
hook is fulfilled.
success(/* result, form */) {
// Do something
}
onerror
The onerror
hook is a promise-aware action which is called when the onsubmit
hook is rejected.
error(/* error, form */) {
// Do something
}
oncancel
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();
}
onvalidate
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();
}
oninvalid
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
}
Fit-Form Component Event Handler Hooks
The form
object is always curried in as the last argument for all
component event handler hooks.
onkeydown
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();
}
}
onkeyup
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.
keypress
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.
Fit-Form Attributes
isUnsubmittable
Returns a Boolean value of the form's (un)submittability.
form.get('isUnsubmittable'); // true
You can still call
submit
ifisUnsubmittable
is true.
isSubmittable
Returns a Boolean value of the form's submittability.
form.get('isSubmittable'); // true
isValid
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
isInvalid
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
isDirty
Returns a Boolean value of the form's state. A dirty form is one with changes.
form.get('isDirty'); // true
isPristine
Returns a Boolean value of the form's state. A pristine form is one with no changes.
form.get('isPristine'); // true
isCancelling
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
isSubmitting
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
isValidating
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
didCancel
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
didSubmit
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
didValidate
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