Validation error highlighting for form fields.
DISCLAIMER: This addon is not actively maintained for public use. Pull requests are welcome, but we do not guarantee responses to bug submissions or feature requests, so use at your own risk.
- Ember.js v3.24 or above
- Ember CLI v3.24 or above
- Node.js v12 or above
ember install @gavant/ember-validations
NOTE: To use the addon styles, you must use SASS:
ember install ember-cli-sass
Upon addon installation, an import statement will be added to your app.scss
:
@import "gavant-ember-validations";
This addon is bundled with ember-changset and ember-changeset-validations, and requires that a Changeset
object be created from the data (e.g. a Ember Data Model
or even just a basic POJO) that you intend to modify and validate in the form.
// app/pods/foo/route.ts
import Route from '@ember/routing/route';
import Validations from 'my-app/validations/my-validations';
export default class FooRoute extends ChangesetRoute(Route) {
validations = Validations;
}
// app/pods/foo/controller.ts
import Controller from '@ember/controller';
import { action } from '@ember/object';
export default class FooController extends Controller {
@action
submit(changeset) {
//do something with the validated changeset
//e.g. `changeset.save()`, etc...
}
}
In more complex forms, you may have multiple changesets that need to be validated at the same time in a single form submission (for example, you have a parent model with validations, and that model then has a dynamic hasMany
relationship of nested models, each of which needs their own changeset). For these cases, you can render "child" validators inside of the parent <FormValidator>
, each of which are passed their own changeset.
When the parent <FormValidator>
's submit
action is invoked, it will validate each of the associated child changesets in addition to its own.
NOTE: Both the <FormValidator::Child>
and <InputValidator>
components should NEVER be used directly. Always use their contextual component version that is yielded by <FormValidator>
.
@changeset: {Changeset}
- (required) The changeset object to validate@submit: {Function}
- (required) An action which will be invoked upon form submission if the validation passes
@changeset: {Changeset}
- (required) The changeset object to validate in the child validator
@errors: {String[]}
- (required) An array of error message strings that will be displayed to the user when editing/submitting the field in an invalid state. Note that in almost all cases, you will pass@errors={{changeset.error.fieldName.validation}}
here, wherefieldName
is the name of the associated property in the changeset, e.g.changeset.error.phoneNumber.validation
.@label: {Boolean}
- (optional, defaults tofalse
) When true, a<label>
will be rendered for the input using the given@fieldLabel
. (When using components like<FlInput>
, you'll usually leave this asfalse
)@fieldLabel: {String}
- (optional) The text to display in the rendered<label>
for the input@hideErrorText: {Boolean}
- (optional, defaults tofalse
) When true, the field's associated error messages will NEVER be shown, even when the field is in invalid state. Useful if you have some other custom error display, or only want the error highlighting styles to show when a field is invalid.@msg: {String}
- (optional) A custom error message string to display instead of the errors passed in via@errors
. Useful if you need a custom error message that is created dynamically or defined outside ofember-changeset-validations
normal constructs.
git clone <repository-url>
cd gavant-ember-validations
npm install
npm run lint:js
npm run lint:js -- --fix
ember test
– Runs the test suite on the current Ember versionember test --server
– Runs the test suite in "watch mode"ember try:each
– Runs the test suite against multiple Ember versions
ember serve
- Visit the test-app application at http://localhost:4200.
This project is licensed under the MIT License.