logo dile-components dile-components

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:

Properties

The form also has the DileForm properties validationMessages, language, novalidate and skipHidden.

Methods

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>