vue-contextable
Contextable.js plugin for Vue.js v2. Form validation has never been easier!
This plugin integrates the contextable.js framework into your Vue.js application.
Contextable.js is a simple, unopinionated and minimalist framework for creating context objects with support for unopinionated ORM, object schemas, type casting, validation and error handling. It has a rich API which significantly simplifies server-side and client-side data validation and manipulation. Because it's an unopinionated framework it flawlessly integrates with popular modules like vuex, apollo-client and other related libraries.
Contextable.js together with Vue.js represents a web framework on steroids. Thanks to its powerful context-aware and flexible model objects, a form validation has never been easier. This plugin brings even more elegant way to do form validation using contextable.js
and still leaves you freedom to customize and fine-tune the integration.
Make sure you check contextable.js API page for details about the framework.
This is a light weight open source package for Vue.js. The source code is available on GitHub where you can also find our issue tracker.
Related Projects
- Contextable.js: Simple, unopinionated and minimalist framework for creating context objects with support for unopinionated ORM, object schemas, type casting, validation and error handling and more.
Installation
Run the command below to install the package.
$ npm install --save vue-contextable contextable
When used with a module system, you must explicitly install vue-contextable
via Vue.use()
.
;; Vue;
Getting Started
Initialize the application context and define a user model.
; const context = ; // context initialization context;
Attach the context to your Vue.js application.
const app = context // injecting context into child components ...;
By passing the context instance to the root Vue
instance as the context
option, the $context
property is injected into every child component.
Form Example
Object validation has been one of the incentives for creating the contextable.js framework. This plugin brings even more elegant way to do form validation using contextable.js
.
<!-- input field --> {{ user.$name.errors | firstMessage }} <!-- buttons --> Add User
Reactive model is an extended instance of a Model class, provided by the contextable.js
, on which Vue.js
can track changes and re-render when a model field changes. You can access reactive model instances by using the this.{dataKey}
syntax (you are actually accessing the data
).
You can manually validate the model by calling the this.{dataKey}.$validate()
method which is asynchronous and returns a Promise
. This is useful when the reactive
options is set to false
.
Integration
It's natural for contextable.js to be flexible and easily integratable with other technologies. Everything that's added to the context instance is automagically available in every model as this.$context.{my-variable}
by default. Vuex, apollo-client and similar technologies represent a common scenario for such integration.
; // imagine that you've already define the vuex store; // imagine that you've already define the vuex store let context = $store apollo;...let user = ;user$context$store; // => vuex store instanceuser$contextapollo; // => apollo client instance
API
This plugin adds some useful features to contextable.js, which help us write even less code.
Component
By passing the context instance to the root Vue
instance as the context
option, the $context
property is injected into every child component. The plugin also defines the contextable
namespace, which can be used for reactive validating of a model.
Model
When a new model is created through the contextable
API within a component, some useful reactive methods and variables are applied.
Model.prototype.$applyErrors(errors): Model
A reactive alternative of the
applyErrors()
method which deeply populates fields with the providederrors
(useful for loading validation errors received from the server).
Option | Type | Required | Default | Description |
---|---|---|---|---|
errors | Array | No | [] | An array of errors. |
Model.prototype.$build(): Model
Rebuilds model's reactivity system.
contextable: validate: dataKey: 'user' modelName: 'User' { thisuserbook = title: 'foo'; // sets field data but the reactivity listeners are removed thisuserbooks = title: 'bar'; // sets field data but the reactivity listeners are removed thisuser; // rebuilds the reactivity system }
Model.prototype.$populate(data): Model
Reactive alternative of the
populate()
method which applies data to a model.
Option | Type | Required | Default | Description |
---|---|---|---|---|
data | Object | Yes | - | Data object. |
contextable: validate: dataKey: 'user' modelName: 'User' { thisuser; }
Model.prototype.$validate({quiet}): Promise(Model)
Reactive alternative of the
validate()
method which validates the model fields and throws a validation error if not all fields are valid unless thequiet
is set to true.
Option | Type | Required | Default | Description |
---|---|---|---|---|
quiet | Boolean | No | false | When set to true , a validation error is thrown. |
Example
An example application is available in the ./example
folder. You can run the npm run example
command to start the server.
Tutorials
See vue-js-cheatsheet for more.
License (MIT)
Copyright (c) 2016 Kristijan Sedlak <xpepermint@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.