VeeValidate 2.1: Validation Providers

A retake on Vue.js form validation

Abdelrahman Awad
Oct 28, 2018 · 5 min read

This article marks the release of vee-validate 2.1.0, a template-based validation framework for Vue.js which mainly uses directives.

Directives in Vue.js offer somewhat low-level access that allows us to manipulate the element bound to the directive.

While very handy to use, directives have limits because they don’t have an instance, or rather a state. Back in Vue 1.x they had state and allowed much more to be done. This was one of the reasons that influenced vee-validate design to be template-based.

Recently I introduced the Verify API which validates values without declaring a field. You can even use the verify method on the server-side. 🤨

import express from 'express';
import bodyParser from 'body-parser';
import { Validator } from 'vee-validate';
const app = express();
app.use(bodyParser.json());
const v = new Validator();
// validate the subscribe request.
function validateReq (req) {
return v.verify(req.body.email, 'required|email');
}
app.post('/subscribe', async (req, res) => {
const { errors, valid } = await validateReq(req);
if (!valid) {
return res.status(422).json({
errors
});
}
// ...
});
app.listen(3000);

This new API has sparked a new idea for me a few weeks ago:

Can I use components to validate? 🤔

Vue has a component API called scoped slots which allows a component to pass data to its slot in an isolated manner. I used that feature for various purposes in our clients’ works.

VueApollo uses them as data providers, which in some cases reduces the JavaScript considerably. Scoped slots offer the ability to create behaviors encapsulated in components and a way to communicate the results.

I began experimenting with render functions, diving into the VNode API. I managed to create a reliable alternative to the directive, I would even say, a better approach.


Validation Provider

This is a component that leverages scoped slots to provide validation for your inputs like Vuetify’s VTextField component. It looks like this:

<ValidationProvider rules="required">
<VTextField slot-scope="{ errors }" v-model="value" :error-messages="errors" />
</ValidationProvider>

Aside from errors, the slot-scope also contains classes, flags, and aria attributes. Opting in for any of those useful properties is better than implicitly injecting them into your components. It also does not force your template to be structured in a certain way. This API is explicit, clean and flexible.

<ValidationProvider rules="required">
<VTextField slot-scope="{ errors }" v-model="foo" :error-messages="errors" />
</ValidationProvider>
<ValidationProvider rules="required">
<VTextField slot-scope="{ errors }" v-model="bar" :error-messages="errors" />
</ValidationProvider>

This can grow to become quite verbose, definitely not pretty in a very large form.

A simple refactoring will make it more attractive. Creating another component wrapped by this one is trivial.

<template>
<ValidationProvider :rules="rules">
<VTextField slot-scope="{ errors }" v-model="innerValue" :error-messages="errors" />
</ValidationProvider>
</template>
<script>
import { ValidationProvider } from 'vee-validate';
export default {
name: 'InputWithValidation',
props: {
rules: [String],
value: null
},
components: {
ValidationProvider
},
data: () => ({
innerValue: null
}),
watch: {
innerValue (val) {
this.$emit('input', val);
}
}
};
</script>

Refactoring the previous example would make it look like this:

<InputWithValidation v-model="foo" rules="required" /><InputWithValidation v-model="bar" rules="required" />

This is a self-validating component but done right. We can also refactor it in another way using higher-order components.


Higher-Order Components

A High-order function is a function that takes a function and returns a new function, an enhanced one.

Likewise, a higher-order component takes a component and returns a new enhanced component. In our case, we want to add validation to our components. This is where withValidation comes in.

import { VTextField } from 'vuetify/lib';
import { withValidation } from 'vee-validate';
const VInput = withValidation(VTextField, ({ errors }) => ({
'error-messages': errors
}));

You can use the new component in place of your input components. The withValidation function uses the ValidationProvider to “enhance” your component.

The second argument transforms the slot-scope data into props which are passed to the original component.

But, there are some cons to using HoC which I will not touch on in this article. Using either approach is fine.

Using components for validation introduces new problems. Like tracking the current validation state without injections/shared state 🤔.

Here is a concrete example:

disable a button as long as inputs are invalid.

We want to be able to observe our inputs and have something present their state to us. At this point, I thought why not double down on the scoped-slots components thing and add another one 🤪.


Validation Observer

This component presents the current state of child providers and their HoC variants.

It looks like this:

<ValidationObserver>
<div slot-scope="{ valid }">
<InputWithValidation v-model="foo" rules="required" />
<InputWithValidation v-model="bar" rules="required" /> <VBtn :disabled="!valid" @click="submit">Submit</VBtn>
</div>
</ValidationObserver>

You can also control them by validating them on demand or resetting them using the public methods on the observer instance.

Here is everything in action 🎉

https://codesandbox.io/s/mjy97x85py

You can find more detailed description over in the docs which cover scopes, manual validation, and form resets.

Perf Matters

By taking advantage of Vue’s virtual DOM and render functions vee-validate is now able to work with ridiculously large forms. larger than before for sure.

A long-standing issue with 800 radio input in the same page would have poor performance, Another that was using 120+ text inputs would cause memory leaks in Edge. Using validation providers had a negligible effect on performance in both cases.

Strictly using these components would eliminate the small overhead of having the global mixin that injects state for the directive.


The Future of VeeValidate

We have experimented with those components in Baianat and so far it is easier to use than the directive, less confusing and everyone seems to love it.

For me, this API lights a bright future for vee-validate. It’s API is smaller than the directive, more reliable, SSR friendly, and efficient 💨.

Currently, I’m working on an experimental branch v3 which removes the old API which reduces the bundle size considerably.

  • Full-bundle went down to 34kb gzipped (32% smaller).
  • Minimal bundle went down to 11kb (57% smaller 😲).

More improvements can be squeezed out once vee-validate begins shifting towards a more modular approach, providing only what you need/use in your apps, not everything.

As for 2.x versions, they will have both approaches available and will continue improving upon the existing API. It will have feature parity with the 3.x versions.

I would love 😍 to get feedback from the community. What do these changes mean to you?

Thanks to Abdelrahman Ismail

Abdelrahman Awad

Written by

JavaScript Developer at Baianat

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade