<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marlon</title>
    <description>The latest articles on DEV Community by Marlon (@marlonfsolis).</description>
    <link>https://dev.to/marlonfsolis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F570533%2F0e4d7c18-17a7-4150-a2fd-570025209137.png</url>
      <title>DEV Community: Marlon</title>
      <link>https://dev.to/marlonfsolis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marlonfsolis"/>
    <language>en</language>
    <item>
      <title>Validating forms in Quasar Framework - Vee-Validate</title>
      <dc:creator>Marlon</dc:creator>
      <pubDate>Tue, 02 Feb 2021 03:26:34 +0000</pubDate>
      <link>https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vee-validate-4e7g</link>
      <guid>https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vee-validate-4e7g</guid>
      <description>&lt;p&gt;On my previous post &lt;a href="https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vuelidate-5efi"&gt;Validating forms in Quasar Framework - Vuelidate&lt;/a&gt; we saw how to validate a &lt;em&gt;Person Registration&lt;/em&gt; form using Vuelidate validation library.&lt;br&gt;
On this post I will show you how to validate the same form using &lt;a href="https://vee-validate.logaretm.com/v3/"&gt;Vee-Validate validation library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;VeeValidate is a validation framework built specifically for Vue.js. &lt;br&gt;
VeeValidate is a collection of function-based APIs and Vue.js components that allow you to validate individual form elements and even the entire form. &lt;/p&gt;

&lt;p&gt;1- Lets start installing the npm package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install vee-validate --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- Once the vee-validate package installation is done and success we need to create a new &lt;strong&gt;Quasar boot&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;quasar new boot vee-validate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- On this boot file we will setup the Vee-Validate and add it to our app. Go to &lt;strong&gt;src/boot&lt;/strong&gt; directory and open &lt;strong&gt;vee-validate.js&lt;/strong&gt; file that was just created. Replace the code on it with the next code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { 
    configure, 
    extend, 
    ValidationObserver, 
    ValidationProvider 
} from 'vee-validate'
import { 
    email, 
    required 
} from 'vee-validate/dist/rules'

export default async ({ Vue }) =&amp;gt; {
  // Register it globally
  Vue.component('ValidationProvider', ValidationProvider)
  Vue.component('ValidationObserver', ValidationObserver)
}

// Add build-in rules
extend('required', required)
extend('email', email)

// Add custom rule
extend('minMaxValue', {
  validate: (value, {
    min,
    max
  }) =&amp;gt; {
    return value &amp;gt;= Number(min) &amp;amp;&amp;amp; value &amp;lt;= Number(max)
  },
  message: (fieldName, {
    min,
    max
  }) =&amp;gt; {
    return `${fieldName} must be between ${min} and ${max}`
  },
  params: ['min', 'max']
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- Now we need to register the new &lt;strong&gt;vee-validate boot&lt;/strong&gt; in our Quasar app. Open &lt;strong&gt;quasar.conf.js&lt;/strong&gt; file and add  &lt;em&gt;'vee-validate'&lt;/em&gt; item in &lt;strong&gt;boot array&lt;/strong&gt;. it should looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//...
boot: [
  'i18n',
  'vee-validate'
],
//...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5- Now we have the Vee-Validate ready to be used in our app. Lets see how to validate a simple &lt;em&gt;Person Registration&lt;/em&gt; form using Vee-Validate.&lt;/p&gt;

&lt;p&gt;a) First create the data properties to be assigned to the form fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data: () =&amp;gt; ({
  name: 'Marlon',
  age: 36,
  email: 'marlon@gmail.com'
}),
methods: {
  onSubmit () {
    console.log('Form submitted')
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) Create a form with 3 field (Name, Age and Email) and assign the data properties to the v-models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;q-form style="width: 60%" @submit.stop="onSubmit"&amp;gt;
  &amp;lt;q-input
    outlined
    label="Name"
    v-model="name"
  &amp;gt;&amp;lt;/q-input&amp;gt;
  &amp;lt;q-input
    outlined
    label="Age"
    v-model="age"
  &amp;gt;&amp;lt;/q-input&amp;gt;
  &amp;lt;q-input
    outlined
    label="Email"
    v-model="email"
  &amp;gt;&amp;lt;/q-input&amp;gt;

  &amp;lt;q-btn label="Submit" type="submit" color="primary"/&amp;gt;
&amp;lt;/q-form&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) Now lets validate the form with the rules we already register on &lt;em&gt;vee-validate.js&lt;/em&gt; file. To do this we just need to wrap our form with &lt;strong&gt;ValidationObserver&lt;/strong&gt; component and each of the form elements with &lt;strong&gt;ValidationProvider&lt;/strong&gt; component.&lt;br&gt;
Replace the form code we did before with the next code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ValidationObserver v-slot="{handleSubmit}"&amp;gt;
  &amp;lt;q-form style="width: 60%" 
          @submit.stop="handleSubmit(onSubmit)"
  &amp;gt;
    &amp;lt;ValidationProvider 
        name="Name" 
        rules="required" 
        v-slot="v"
    &amp;gt;
      &amp;lt;q-input
        outlined
        label="Name"
        v-model="name"
        :error="v.errors.length &amp;gt; 0"
        :error-message="v.errors[0]"
      &amp;gt;&amp;lt;/q-input&amp;gt;
    &amp;lt;/ValidationProvider&amp;gt;

    &amp;lt;ValidationProvider
        name="Age" 
        rules="required|minMaxValue:1,180" 
        v-slot="v"
    &amp;gt;
      &amp;lt;q-input
        outlined
        label="Age"
        v-model="age"
        :error="v.errors.length &amp;gt; 0"
        :error-message="v.errors[0]"
      &amp;gt;&amp;lt;/q-input&amp;gt;
    &amp;lt;/ValidationProvider&amp;gt;

    &amp;lt;ValidationProvider
        name="Email" 
        rules="required|email" 
        v-slot="v"
    &amp;gt;
      &amp;lt;q-input
        outlined
        label="Email"
        v-model="email"
        :error="v.errors.length &amp;gt; 0"
        :error-message="v.errors[0]"
      &amp;gt;&amp;lt;/q-input&amp;gt;
    &amp;lt;/ValidationProvider&amp;gt;

    &amp;lt;q-btn label="Submit" type="submit" color="primary"/&amp;gt;
  &amp;lt;/q-form&amp;gt;
&amp;lt;/ValidationObserver&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;handleSubmit&lt;/strong&gt; slot method on &lt;em&gt;ValidationObserver&lt;/em&gt; prevent the form be submitted with not valid elements (elements with validation error).&lt;/p&gt;

&lt;p&gt;Now lets display a message when the form is submitted with all the data correct without validation errors. Remember that the &lt;em&gt;onSubmit&lt;/em&gt; method will be executed only if all fields on the form are valid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onSubmit () {
  this.$q.notify({
    color: 'green-4',
    textColor: 'white',
    icon: 'cloud_done',
    message: 'Form submitted'
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done, we have validated or form using the Vee-Validate validation framework.&lt;/p&gt;

&lt;p&gt;Have fun with Quasar validating your future forms with Vee-Validate!&lt;/p&gt;

&lt;p&gt;References&lt;br&gt;
&lt;a href="https://quasar.dev/"&gt;https://quasar.dev/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://vee-validate.logaretm.com/v3/"&gt;https://vee-validate.logaretm.com/v3/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>quasar</category>
      <category>vue</category>
      <category>veevalidate</category>
    </item>
    <item>
      <title>Validating forms in Quasar Framework - Vuelidate</title>
      <dc:creator>Marlon</dc:creator>
      <pubDate>Tue, 02 Feb 2021 02:27:10 +0000</pubDate>
      <link>https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vuelidate-5efi</link>
      <guid>https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vuelidate-5efi</guid>
      <description>&lt;p&gt;Quasar is an MIT licensed open-source Vue.js based framework, which allows you as a web developer to quickly create responsive websites/apps in many flavours like SPA, SSR, PWAs and more.&lt;br&gt;
Quasar is the number one solution based on Vue.js whether you are only building a desktop website, a desktop app, a mobile app, or even all of them.&lt;/p&gt;

&lt;p&gt;Before submitting data to the server, it is important to ensure all required form controls are filled out, in the correct format. This is called client-side form validation, and helps ensure data submitted matches the requirements set forth in the various form controls. &lt;br&gt;
Client-side validation is an initial check and an important feature of good user experience; by catching invalid data on the client-side, the user can fix it straight away. If it gets to the server and is then rejected, a noticeable delay is caused by a round trip to the server and then back to the client-side to tell the user to fix their data.&lt;/p&gt;

&lt;p&gt;Quasar provide an internal validation solution that work perfect on any situation. But if you want to go with a more sophisticated and advanced validation system you can choose any of the validation library made for Vue.js that exist out there.&lt;br&gt;
Quasar recommend &lt;a href="https://vuelidate.js.org/"&gt;Vuelidate&lt;/a&gt; but I do prefer other library called &lt;a href="https://vee-validate.logaretm.com/v3/"&gt;Vee-Validate&lt;/a&gt;. &lt;br&gt;
What I do not like about Vuelidate is that do not feels natural Vue making you pass to v-model $v.name.$model (and other things like this) instead the original data property name.&lt;br&gt;
Vee-Validate feels natural using nothing but components that wrap the form and fields you need to validate.&lt;br&gt;
In this post we are going to see how to setup and how validate a simple form using &lt;em&gt;Vuelidate&lt;/em&gt;. On a next post &lt;a href="https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vee-validate-4e7g"&gt;Validating forms in Quasar Framework - Vee-Validate&lt;/a&gt; I will show how to use Vee-Validate.&lt;/p&gt;

&lt;p&gt;1- Lets start installing the Vuelidate npm package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install vuelidate --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- Once the vuelidate package installation is done and success we need to create a new &lt;strong&gt;Quasar boot&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;quasar new boot vuelidate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- On this boot file we will setup the Vuelidate and add it to our app. Go to &lt;strong&gt;src/boot&lt;/strong&gt; directory and open &lt;strong&gt;vuelidate.js&lt;/strong&gt; file that was just created. Replace the code on it with the next code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Vuelidate from 'vuelidate'

export default async ({ Vue }) =&amp;gt; {
  Vue.use(Vuelidate)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- Now we need to register the new &lt;strong&gt;vuelidate boot&lt;/strong&gt; in our Quasar app. Open &lt;strong&gt;quasar.conf.js&lt;/strong&gt; file and add  &lt;em&gt;'vuelidate'&lt;/em&gt; item in &lt;strong&gt;boot array&lt;/strong&gt;. it should looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//...
boot: [
  'i18n',
  'vuelidate'
],
//...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5- Now we have the Vuelidate ready to be used in our app. Lets see how to validate a simple person registration form using Vuelidate.&lt;/p&gt;

&lt;p&gt;a) First create the data properties to be assigned to the form fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data: () =&amp;gt; ({
  name: 'Marlon',
  age: 36,
  email: 'marlon@gmail.com'
}),
methods: {
  onSubmit () {
    console.log('Form submitted')
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) Create a form with 3 field (Name, Age and Email) and assign the data properties to the v-models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;q-form style="width: 60%" @submit.stop="onSubmit"&amp;gt;
  &amp;lt;q-input
    outlined
    label="Name"
    v-model="name"
  &amp;gt;&amp;lt;/q-input&amp;gt;
  &amp;lt;q-input
    outlined
    label="Age"
    v-model="age"
  &amp;gt;&amp;lt;/q-input&amp;gt;
  &amp;lt;q-input
    outlined
    label="Email"
    v-model="email"
  &amp;gt;&amp;lt;/q-input&amp;gt;

  &amp;lt;q-btn label="Submit" type="submit" color="primary"/&amp;gt;
&amp;lt;/q-form&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) Now lets import the validator from Vuelidate&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { 
  email, 
  maxValue, 
  minValue, 
  numeric, 
 required 
} from 'vuelidate/lib/validators'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;d) And create the validation rules. After &lt;em&gt;methods&lt;/em&gt; property add the &lt;em&gt;validations&lt;/em&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;methods: {...},
validations: {
  name: {
    required
  },
  age: {
    required,
    numeric,
    min: minValue(1),
    max: maxValue(180)
  },
  email: {
    required,
    email
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;e) We have created the rules to validate the fields. Note that the rules must have the same name of the data property that we are validating.&lt;br&gt;
Now we need to add the validation rules to the form. Modify the form with the next code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;q-form style="width: 60%" @submit.stop="onSubmit"&amp;gt;
  &amp;lt;q-input
    outlined
    label="Name"
    v-model="$v.name.$model"
    :error="$v.name.$error" 
    error-message="Field required"
  &amp;gt;&amp;lt;/q-input&amp;gt;
  &amp;lt;q-input
    outlined
    label="Age"
    v-model="$v.age.$model"
    :error="$v.age.$error"
    error-message="Age most be between 1 and 180"
  &amp;gt;&amp;lt;/q-input&amp;gt;
  &amp;lt;q-input
    outlined
    label="Email"
    v-model="$v.email.$model"
    :error="$v.email.$error"
    error-message="Invalid email address"
  &amp;gt;&amp;lt;/q-input&amp;gt;

  &amp;lt;q-btn label="Submit" type="submit" color="primary"/&amp;gt;
&amp;lt;/q-form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;f) And the last thing is validate the form data that is been sent to the &lt;strong&gt;onSubmit&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onSubmit () {
  console.log(this.$v)

  if (this.$v.$anyError) {
    this.$q.notify({
      color: 'red-4',
      textColor: 'white',
      icon: 'warning',
      message: 'Form not valid'
    })
  } else {
    this.$q.notify({
      color: 'green-4',
      textColor: 'white',
      icon: 'cloud_done',
      message: 'Form submitted'
    })
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done, we have validated a form using Vuelidate library. &lt;br&gt;
On a next post &lt;a href="https://dev.to/marlonfsolis/validating-forms-in-quasar-framework-vee-validate-4e7g"&gt;Validating forms in Quasar Framework - Vee-Validate&lt;/a&gt; we will validate the same form using Vee-Validate library.&lt;/p&gt;

&lt;p&gt;References&lt;br&gt;
&lt;a href="https://quasar.dev/"&gt;https://quasar.dev/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation"&gt;https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/quasar-framework/adding-validation-to-quasar-based-forms-5391cee48c20"&gt;https://medium.com/quasar-framework/adding-validation-to-quasar-based-forms-5391cee48c20&lt;/a&gt;&lt;/p&gt;

</description>
      <category>quasar</category>
      <category>vue</category>
      <category>vuelidate</category>
      <category>veevalidate</category>
    </item>
  </channel>
</rss>
