Custom Validation
Field
The Field component accepts a @validate
property, allowing you to define a callback function for custom field validation. Read more about addError.
Parameters
name
(string): The name of the form field being validated.value
(string): The value of the form field being validated.data
(Object): The data object containing additional information for validation.handlers
(Object): An object containing handler functions.handlers.addError
(Function): A function to add an error if validation fails.
Example
validateUsername(name, value, data, { addError }) {
if (data.bar / 2 === value) {
addError(name, { title: I18n.t(`foo.bar.${name}`), message: "That's not how maths work." });
}
}
<form.Field @name="username" @validate={{this.validateUsername}} />
Form
The Form component accepts a @validate
property, allowing you to define a callback function for custom form validation.
Parameters
data
(Object): The data object containing additional information for validation.handlers
(Object): An object containing handler functions.handlers.addError
(Function): A function to add an error if validation fails.
Example
validateForm(data, { addError }) {
if (data.bar / 2 === data.baz) {
addError(name, { title: I18n.t(`foo.bar.${name}`), message: "That's not how maths work." });
}
}
<Form @validate={{this.validateForm}} />