2.0.1 • Published 3 years ago

quasar-app-extension-vuelidate-rules-v2 v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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 ext add vuelidate-rules

Quasar CLI will retrieve it from NPM and install the extension.

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.

  • If no parameters are needed:
    $rules.alpha() 
    $rules.alpha('Please use only letters')
  • If paramets are needed:
    $rules.between(10,100)
    $rules.between(10,100, 'Please enter a number between 0 and 100')

Available methods

ruleparametersdescription
requirednoneRequires non-empty data. Checks for empty arrays and strings containing only whitespaces.
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
  • All methods receive a last optional parameter with the customized error message.

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

  • Make locator based methods like sameAs or requiredIf to work, or develop an alternative

Related projects

2.0.1

3 years ago

2.0.0

3 years ago