logo dile-components dile-components

Rules reference

These rules are available in forms with the DileFormValidation mixin. See Declaring rules to learn how to use them, and Custom rules to add your own.

Built-in rules

Rules other than required, requiredIf and accepted are skipped when the value is empty: use required to make a field mandatory. Numeric rules convert numeric strings, like the values of dile-input-integer, dile-input-number, dile-input-money or dile-input-percentage, which keep value normalized with . as decimal separator while typing, so the rules work before the blur.

Rule Example Description
required required The field must have a value. Same criterion as the required attribute: whitespace-only text and empty arrays are empty, false is a value.
requiredIf requiredIf:type,company Required when another field has one of the values. Without values, when the other field is not empty.
accepted accepted The value must be true, "true", 1, "1", "yes" or "on".
email email A valid email address.
url url A valid http or https URL.
numeric numeric A number (decimal point, not comma).
integer integer A whole number.
decimal decimal:0,2 A number with a decimal point and between min and max decimal places, like Laravel's decimal. With one parameter (decimal:2), exactly that many. 12, 12.5, 12. and .5 pass decimal:0,2; 12.345 and 12,5 do not.
min min:18 A number equal to or greater than the parameter.
max max:99 A number equal to or less than the parameter.
between between:1,10 A number between both parameters, included.
gt gt:0, gt:start A number greater than the parameter. Like in Laravel, a parameter that is not a number is the name of another field, and the message shows its label. When that field has no number, the rule passes.
gte gte:0, gte:start A number greater than or equal to the parameter, or to another field.
lt lt:100, lt:end A number less than the parameter, or than another field.
lte lte:100, lte:end A number less than or equal to the parameter, or to another field.
minLength minLength:3 At least this number of characters, or items in arrays.
maxLength maxLength:120 No more than this number of characters, or items in arrays.
pattern pattern:[A-Z]{3} A regular expression. As in the HTML pattern attribute, a string must match the whole value. A RegExp (schema only) is used as it is.
in in:draft,published One of the values.
notIn notIn:admin,root None of the values.
same same:password The same value as another field.
different different:old_password A different value from another field.
prohibitedUnless prohibitedUnless:vat,0 The field must be empty unless another field has one of the values, like Laravel's prohibited_unless.
file file The value must be a file, or a list of files.
fileType fileType:image/*,.pdf Allowed file types, with the syntax of the HTML accept attribute.
maxFileSize maxFileSize:2MB Maximum size of each file.
minFileSize minFileSize:10KB Minimum size of each file.
image image Each file must be an image the browser can read.
minImageWidth minImageWidth:800 Minimum width of each image, in pixels.
maxImageWidth maxImageWidth:4000 Maximum width of each image, in pixels.
minImageHeight minImageHeight:600 Minimum height of each image, in pixels.
maxImageHeight maxImageHeight:3000 Maximum height of each image, in pixels.

File rules

The file rules validate fields whose value is a File or a list of files, like dile-drop-file, also with multiple: every file must pass.

<dile-drop-file name="photo" label="Photo" validations="image|maxFileSize:2MB|minImageWidth:800|maxImageWidth:4000"></dile-drop-file>
<dile-drop-file name="attachments" label="Attachments" multiple validations="fileType:.pdf,image/*|maxFileSize:5MB|maxLength:3"></dile-drop-file>

The helpers parseFileSize(size), formatFileSize(bytes) and readImageDimensions(file) are exported by the package.

Spanish and banking rules

These rules are not built in. Import their module once, for example in the entry point of the application, to register them with their messages in English and Spanish:

import '@dile/validation/rules/spain.js';
import '@dile/validation/rules/banking.js';
Rule Module Example Description
spanishFiscalId spain.js spanishFiscalId, spanishFiscalId:country_code A Spanish tax ID: NIF, NIE or CIF, with its control letter or digit. Lowercase letters are accepted.
spanishPostalCode spain.js spanishPostalCode:country_code,province Five digits starting with a province code (01–52). With a second field, the code must belong to the province in that field (its two-digit code).
iban banking.js iban, iban:sepa An IBAN: format and check digits (MOD 97-10). With sepa, only countries of the SEPA scheme. Spaces and lowercase letters are accepted.
bic banking.js bic A BIC/SWIFT code (ISO 9362): 8 or 11 uppercase characters, the first 6 letters.
sepaMandateReference banking.js sepaMandateReference A SEPA mandate reference (UMR): up to 35 characters of the SEPA set, without / at the start or the end, nor //.

For an IBAN field with dile-input-mask, give the mask the length of the longest IBAN you accept and minChars the shortest one, so the mask helps to type and the rule checks the IBAN:

<dile-input-mask
  name="iban"
  label="IBAN"
  mask="AA00 XXXX XXXX XXXX XXXX XXXX XXXX XXX"
  minChars="15"
  lazySeparators
  validations="iban:sepa"
></dile-input-mask>

Try the rules

Every field of this form is validated while you type (validate-on="input"), and shows its rules as help text. The IBAN field uses the banking rules, imported once.

<script type="module">
  import { LitElement, html } from 'lit';
  import { DileForm } from '@dile/ui/mixins/form';
  import { DileFormValidation } from '@dile/validation';
  import '@dile/validation/rules/banking.js';
  import '@dile/ui/components/input/input.js';

  class RulesPlayground extends DileFormValidation(DileForm(LitElement)) {
    static validations = {
      email: 'email',
      web: 'url',
      age: 'integer|between:18,120',
      price: 'decimal:0,2|gt:0',
      max_price: 'gte:price',
      slug: 'pattern:[a-z0-9-]+|maxLength:20',
      iban: 'iban:sepa',
    };

    constructor() {
      super();
      this.validateOn = 'input';
    }

    render() {
      return html`
        <dile-input name="email" label="Email" message="email"></dile-input>
        <dile-input name="web" label="Website" message="url"></dile-input>
        <dile-input name="age" label="Age" message="integer|between:18,120"></dile-input>
        <dile-input name="price" label="Price" message="decimal:0,2|gt:0"></dile-input>
        <dile-input name="max_price" label="Maximum price" message="gte:price"></dile-input>
        <dile-input name="slug" label="Slug" message="pattern:[a-z0-9-]+|maxLength:20"></dile-input>
        <dile-input name="iban" label="IBAN" message="iban:sepa"></dile-input>
      `;
    }
  }
  customElements.define('rules-playground', RulesPlayground);
</script>
<rules-playground></rules-playground>