Home

Awesome

Quasar App Extension Vuelidate Rules

The elegance of Quasar's internal validation with the power of Vuelidate's built-in validators.

Quasar Framework offers a very practical way to validate form fields, using the rules attribute in Field wrapper, input and select component. The attribute accepts an array of functions that returns true or a string with an error message.

In most cases this is enough, but in some cases you need more complex validations. Quasar's documentation recommends Vuelidate as an external validation solution, but using this approach has a downside: You cannot use external validation to validate child components of a Form.

Leveraging the power of Vuelidate also requires more code to set up a validation, add the validations property to the component to define the rules and bind the error and error-message attributes of the component.

This extension exposes Vuelidate methods so they can be used as Quasar internal rules.

More information about Quasar Validation: https://quasar.dev/vue-components/field#Validation and Form component: https://quasar.dev/vue-components/form

Install

Quasar 2 compatible ( => version 0.4.0 )

quasar ext add vuelidate-rules

For Quasar 1, please use version 0.3.2

Usage

The extension will inject the $rules object to the Vue instance, the rules will then be available in the components. The customized error message is optional, always as the last parameter, and if not provided it will return false.

Available methods

ruleparametersdescription
requirednoneRequires non-empty data. Checks for empty arrays and strings containing only whitespaces.
requiredIfrefRequires non-empty data only if the reference evaluates to true.
requiredUnlessrefRequires non-empty data only if the reference evaluates to false.
minLengthlength integerRequires the input to have a minimum specified length, inclusive. Works with arrays.
maxLengthlength integerRequires the input to have a maximum specified length, inclusive. Works with arrays.
minValuevalue integerRequires entry to have a specified minimum numeric value or Date.
maxValuevalue integerRequires entry to have a specified maximum numeric value or Date.
betweenmin integer, max integerChecks if a number or Date is in specified bounds. Min and max are both inclusive.
alphanoneAccepts only alphabet characters.
alphaNumnoneAccepts only alphanumerics.
numericnoneAccepts only numerics.
integernoneAccepts positive and negative integers.
decimalnoneAccepts positive and negative decimal numbers.
emailnoneAccepts valid email addresses. Keep in mind you still have to carefully verify it on your server, as it is impossible to tell if the address is real without sending verification email.
ipAddressnoneAccepts valid IPv4 addresses in dotted decimal notation like 127.0.0.1.
macAddressseparator string, (default ':')Accepts valid MAC addresses like 00:ff:11:22:33:44:55
urlnoneAccepts only URLs.
isTruenoneOnly accepts a true value
orother rulesPasses when at least one of provided rules passes
andother rulesPasses when all of provided validators passes.
notrulePasses when provided validator would not pass
isvaluePasses when the field value is the same as the provided value, for example true
sameAsvaluePasses when field value is the same as other field value

Examples

Using several rules

<q-input
    v-model="form.name"
    label="Your name *"
    hint="Name and surname"
    :rules="[
      $rules.required('Your name is required'),
      $rules.alpha('Your name should not have numbers'),
      $rules.minLength(3, 'Your name should have at least 3 letters'),
      $rules.maxLength(10, 'Your name should not be larger than 10 letters') 
    ]"
/>

Using logic operators

<q-input
    v-model="form.age"
    label="Your age *"
    :rules="[
      $rules.or($rules.between(10,15), $rules.between(20,25)),
    ]"
/>
<q-input
    v-model="form.age"
    label="Your age *"
    :rules="[
      $rules.or(
        $rules.and(
          $rules.between(10,17),
          $rules.between(16,25)
        ),
        $rules.minValue(17),
        'Or nd message 2'
      ),
    ]"
  />

Modified methods

These methods are not the original Vuelidate methods but modified versions

TBD

Other not Vuelidate methods

Check if is a specific value. This is not a method of Vuelidate but may be useful, so it was added. Can be tied to other data properties, for example to check if a password is the same as the confirmation (not reactive yet)

<q-field
        :value="form.accept"
        label="Please read the license"
        stack-label
        :rules="[
           $rules.is(true, 'Please accept the license')
        ]"
      >
        <q-toggle v-model="form.accept" label="I accept the license and terms" />
</q-field>

To do

Related projects