A very simple validation addon using a custom modifier. This makes adding custom validations to form elements as simple as adding a modifier to the field along with your own helper or validation function.
- Ember.js v3.28 or above
- Ember CLI v3.28 or above
- Node.js v14 or above
ember install ember-validity-modifier
export default MyComponent extends Component {
@action
validate({ value }) {
return value === 'foobar' ? [] : ['Must be a foobar'];
}
}
export default helper(function validateFoobar() {
return ({ value }) => value === 'foobar' ? [] : ['Must be a foobar'];
});
By default validation will happen on change
, input
, and blur
. Comma separate event names.
In cases where we don't have easy access to the form field itself we can also add it to a parent element. This might be the case when adding a modifier that gets applied by another component via its ...attributes
.
import Component from '@glimmer/component';
import { validate } from 'ember-validity-modifier';
export default class MyForm extends Component {
@action
handleSubmit({ target: form }) {
console.log('Fake submit action', Object.fromEntries(new FormData(form)));
}
}
To validate the form state on initial render add validateImmediately=true
.
To validate the form state when initial render and any time one of its dependent arguments change, add the 'validateTracked'
argument with the dependent properties.
Because the validator argument is a function it is possible to not exercise the tracked properties and thus miss out on validations when those tracked properties change. This is the case of the fn
helper which lazy executes thus doesn't trigger Ember's auto-tracking if it isn't ran first.
To compensate we can use validateTracked
to inform the modifier that it needs to run the validations when these properties change.
To validate the form state any time any of its dependent arguments change, add the validateTracked
argument using the array
helper and a list of dependent properties.
export default helper(function validateSelectedOption() {
return ({ name, value }) => value === '' ? [`Must pick an option for ${name}`] : [];
});
export default helper(function validateChangeset([changeset, prop]) {
return async () => {
await changeset.validate(prop);
let { validation: error } = changeset.error[prop] ?? {};
return error ? [error] : [];
};
});
form-error exposes the following:
update
— action to process a validated eventset
— action that can set specific fieldsfor.<name>
— the errors as an arraynative.<name>
— any native errors as an arraycustom.<name>
— any custom errors as an arraymessage.<name>
— thevalidationMessage
from the DOM element
/* All the things */
:valid { … }
:invalid { … }
/* Not on first render, use the validated event to set dataset.validated */
[data-validited]:valid { … }
[data-validited]:invalid { … }
The blog post Managing validity in forms takes a dive into a simple native (vanilla) implementation of this idea. In the post it describes the idea that validations can be managed through DOM events. By attaching the validation functions to an event handler they can easily manage the native custom validity of the element.
When a validate event is dispatched (by default the events are validate
,
input
, change
, and blur
). Each validator function registered will be
evaluated, the results will be consolidated, and the element's custom validity
is set, finally a validated
event is dispatched to announce that the process
is complete (in case of asynchronous validations).
See the Contributing guide for details.
This project is licensed under the MIT License.