logo dile-components dile-components

Declaring rules

The rules of @dile/validation need a form with the DileFormValidation mixin. This page explains how to declare them; the Rules reference lists every rule.

Sources of the rules

The rules of a field come from the first of these sources that has them:

  1. A schema set with setValidationSchema(schema), for example from the CRUD configuration.
  2. The static validations property of the form class.
  3. The validations attribute of the field.

The attribute uses a string with the rules separated by |, and the parameters after :, separated by commas:

<dile-input name="code" label="Code" validations="required|pattern:[A-Z]{3}[0-9]{2}"></dile-input>

The schema accepts the same strings, or arrays where each item is:

Use the array syntax for patterns that contain |, since in strings | separates rules.

A schema entry can also be an object { rules, dependsOn, when }, where rules is any of the formats above, dependsOn lists the fields it depends on (see Fields that depend on other fields) and when is a condition to check the rules (see Conditional rules).

Array fields (name="items[]") use the rules of items (or items.*) for every element.

Note: required can be used as an attribute or as a rule. The attribute is preferable, because it also shows the required indicator and sets aria-required.

Conditional rules

Use when for rules that only apply in some cases, like exclude_unless or Rule::when() in Laravel. It is a function that receives the validation context (value, valueOf(), form, field...) and returns true (or a promise of true) when the rules must be checked. Otherwise the field is valid:

static validations = {
  dir3_code: {
    rules: ['required', 'pattern:[A-Z0-9]{9}'],
    when: ({ valueOf }) => valueOf('is_public_administration') === true,
  },
};

The fields read with valueOf() in when are dependencies of the field, so changing them validates it again (see Fields that depend on other fields). when only applies to the rules: a required attribute of the field is always checked, so use the required rule inside the entry, or bind the attribute to the same condition. Validator.validate() honors when too.

Examples

The three sources side by side:

class CustomerForm extends DileFormValidation(DileForm(LitElement)) {
  // 2. Static property of the form class
  static validations = {
    email: 'email|maxLength:120',
    vat_number: { rules: ['required', 'spanishFiscalId:country'], when: ({ valueOf }) => valueOf('type') === 'company' },
  };

  render() {
    return html`
      <dile-input name="email" label="Email" required></dile-input>
      <!-- 3. Attribute of the field -->
      <dile-input name="web" label="Website" validations="url"></dile-input>
      ...
    `;
  }
}

// 1. Schema, for example with different rules for insert and update
form.setValidationSchema({ password: 'required|minLength:8' }, { operation: 'insert' });

Declare the rules in one place for each form. With the full CRUD, the resource configuration is the natural place.