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.
Please note that
ember-fit-formdoes not provide form control components. It is simply an html form element with abstractions for state management.
- Ember.js v3.16 or above
- Ember CLI v2.13 or above
- Node.js v10 or above
ember install ember-fit-form
// my-form.js
rollback() {
return model.rollbackAttributes();
},
save() {
return model.save();
}This addon supports various data and validation libraries for use within your application's forms. You can even write your own custom adapters.
Built-in Adapters
-
ember-changeset(default)ember-changeset model adapter
Supported Validation Libraries:
-
ember-modelember-data model adapter
Supported Validation Libraries:
Each form adapter defines its own form action hooks, such as onsubmit and oncancel. The default behavior of these actions can be overwritten by passing onsubmit and oncancel into the component invocation. Please see source code for specifics on an adapter's default action hooks.
ex: onsubmit not passed to component
When onsubmit is not explicitly passed into the component, the adapter's default onsubmit action will be called on form submission.
ex: onsubmit passed to component
When onsubmit is explicitly passed into the component, it will call this action in favor of the adapter's default onsubmit action on form submission.
By default, ember-fit-form uses the ember-changeset adapter which expects Changeset models. To setup your default Model, you should configure the addon through config/environment:
module.exports = function(environment) {
var ENV = {
emberFitForm: {
adapter: 'ember-changeset' // default
}
}
}In the case that your forms use various Models throughout the application, you can overwrite the adapter at the component level.
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
onsubmithook will never be called ifonvalidatehooks 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();Fit-Form adapters each contain action hooks. Some hooks call default functions, to reduce overall boilerplate code. For example, the ember-changeset adapter's onsubmit hook calls changeset.save() on each changeset by default. Declaring an onsubmit action on the component will override this behavior.
See default component action hook behavior:
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 this.model.save();
}The
onsubmithook will not be called on form submission ifonvalidatehooks 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 this.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 this.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();
}
}
return true;to bubble the event. This is useful if you still want the form to handle the submit event.
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.isUnsubmittable; // trueYou can still call
submitifisUnsubmittableis true.
Returns a Boolean value of the form's submittability.
form.isSubmittable; // trueReturns a Boolean value of the form's validity. A valid form is one where all of the form's models are valid.
form.isValid; // trueReturns 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.isInvalid; // trueReturns a Boolean value of the form's state. A dirty form is one with changes.
form.isDirty; // trueReturns a Boolean value of the form's state. A pristine form is one with no changes.
form.isPristine; // trueReturns 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.isCancelling; // trueReturns 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.isSubmitting; // trueReturns 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.isValidating; // trueReturns a Boolean value of the form's cancelled state. A cancelled form is one where the oncancel hooks is settled.
form.didCancel; // trueReturns a Boolean value of the form's submitted state. A submitted form is one where the onsubmit hooks is settled.
form.didSubmit; // trueReturns 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.didValidate; // trueGenerate a form adapter
$ ember generate form-adapter foo-bar
This creates app/form-adapters/foo-bar.js and a unit test at tests/unit/form-adapters/foo-bar-test.js. By default, the form-adapter extends the BaseAdapter.
In this example, we'll extend the ember-changeset form-adapter. We will overwrite the default oncancel action to never call rollbackAttributes() on the Changesets.
-
Generate
form-adapterember g form-adapter ember-changeset/no-rollbacks
-
Extend the
ember-changesetform-adapter// app/form-adapters/ember-changeset/no-rollbacks; import EmberChangesetAdapter from 'ember-fit-form/form-adapters/ember-changeset'; export default class EmberChangesetNoRollbacksAdapter extends EmberChangesetAdapter { oncancel() { /* noop - ie. no rollbackAttributes */ } }
-
Define adapter on component or configuration
This project is licensed under the MIT License.