DileFormValidation
Mixin that adds validation rules and live validation to a DileForm element. It lives in its own package, @dile/validation, so applications only load it in the forms that use it.
Without this package, DileForm already checks required fields and the fields' own errors (an impossible date, an incomplete mask...). See Getting started. This mixin adds rules like email, minLength or pattern, custom rules and validation while the user fills the form.
Installation
npm i @dile/validation
Usage
Apply the mixin on top of DileForm and declare the rules of each field:
import { LitElement, html } from 'lit';
import { DileForm } from '@dile/ui/mixins/form';
import { DileFormValidation } from '@dile/validation';
import '@dile/ui/components/input/input.js';
class SignupForm extends DileFormValidation(DileForm(LitElement)) {
static validations = {
email: 'required|email|maxLength:120',
age: ['integer', ['between', 18, 120]],
password_confirmation: 'same:password',
};
render() {
return html`
<dile-input name="email" label="Email"></dile-input>
<dile-input name="age" label="Age"></dile-input>
<dile-input name="password" label="Password" type="password" required validations="minLength:8"></dile-input>
<dile-input name="password_confirmation" label="Repeat password" type="password"></dile-input>
`;
}
}
customElements.define('signup-form', SignupForm);
Then validate the form before using its data:
const { valid, errors } = await form.validate();
validate() is the method of DileForm: it checks, for each field, the required attribute, the field's own validation and then the rules of this mixin. The first error of each field is shown in the field.
Guides
The features of this mixin are explained in the guides of the Validation section:
- Declaring rules: the
validationsattribute, the staticvalidationsproperty, schemas and conditional rules. - Rules reference: built-in rules, file rules, Spanish and banking rules.
- Live validation:
validateOn, debounce and fields that depend on other fields. - Messages and languages:
validationMessages,registerMessages()and the language of the messages. - Custom rules:
registerRule(), local rules and inline rules. - Validation in the CRUD and Validating data without a form.
Properties
- validateOn: String,
eager(default),blur,inputorsubmit. See Live validation. - rules: Object, local rules of the form,
{ ruleName: rule }. See Custom rules. - validateDebounce: Number, default
0. Milliseconds to wait after the last change before a live validation. A field can override it with thevalidate-debounceattribute. - validationOperation: String, the
operationpassed tosetValidationSchema(), if any.
The form also has the DileForm properties validationMessages, language, novalidate and skipHidden.
Methods
- setValidationSchema(schema, { operation, messages }): sets a schema
{ field: rules }that takes precedence over the static validations and the attributes.nullremoves it. The optionalmessagestake precedence overvalidationMessages, also for therequiredattribute. - getValidationRules(name, node): returns the rules of a field.
- getValidationDependencies(name, node): returns the fields declared as dependencies of a field, with
dependsOnorvalidate-depends-on. The fields read withvalueOf()are detected on their own and are not included. - getValidateOn(node): returns the live validation mode of a field.
- getValidateDebounce(node): returns the live validation debounce of a field, in milliseconds.
DileFormValidation demos
Signup form
<script type="module">
import { LitElement, html } from 'lit';
import { DileForm } from '@dile/ui/mixins/form';
import { DileFormValidation } from '@dile/validation';
import '@dile/ui/components/input/input.js';
import '@dile/ui/components/checkbox/checkbox.js';
import '@dile/ui/components/button/button.js';
class DemoSignupForm extends DileFormValidation(DileForm(LitElement)) {
static validations = {
email: 'email|maxLength:120',
age: ['integer', ['between', 18, 120]],
password: 'minLength:8',
password_confirmation: 'same:password',
};
static properties = { result: { type: String } };
render() {
return html`
<dile-input name="email" label="Email" required></dile-input>
<dile-input name="age" label="Age"></dile-input>
<dile-input name="password" label="Password" type="password" required></dile-input>
<dile-input name="password_confirmation" label="Repeat password" type="password" required></dile-input>
<dile-input name="code" label="Code (three uppercase letters)" validations="pattern:[A-Z]{3}" validate-on="input"></dile-input>
<p><dile-checkbox name="terms" required>I accept the terms</dile-checkbox></p>
<dile-button @click=${this.send}>Send</dile-button>
<p>${this.result}</p>
`;
}
async send() {
const { valid } = await this.validate();
this.result = valid ? `Valid data: ${JSON.stringify(this.getData())}` : 'Please review the form';
}
}
customElements.define('demo-signup-form', DemoSignupForm);
</script>
<demo-signup-form></demo-signup-form>
dile-components