k-form
v1.0.3
Published
beta 0.1
Readme
kenry-form
Form validation for Vue.js 2.2+
Install
Available through npm as kenry-form.
import KenryForm from 'kenry-form';
// or var KenryForm = require('kenry-form') or window.KenryForm if you are linking directly to the dist file
// install globally
Vue.use(KenryForm);
Vue.use(KenryForm, options);
### Usage
Once installed you have access to four components (`kenry-form`, `validate`) for managing form state, validating form fields and displaying validation messages.
Example
```html
<div id="app">
<kform :state="formstate" @submit.prevent="onSubmit">
<span>Name *</span>
<input v-model="model.name" required name="name" />
<button type="submit">Submit</button>
</kform>
<pre>{{ formstate }}</pre>
</div>Vue.use(KenryForm);
new Vue({
el: '#app',
data: {
formstate: {},
model: {
name: '',
email: 'invalid-email'
}
},
methods: {
onSubmit: function () {
if(this.formstate.$invalid) {
// alert user and exit early
return;
}
// otherwise submit form
}
}
});The output of formstate will be:
{
"$dirty": false,
"$pristine": true,
"$valid": false,
"$invalid": true,
"$submitted": false,
"$touched": false,
"$untouched": true,
"$focused": false,
"$pending": false,
"$error": {
// fields with errors are copied into this object
},
"$submittedState": {
// each form sumbit, state is cloned into this object
},
"name": {
"$name": "name",
"$dirty": false,
"$pristine": true,
"$valid": false,
"$invalid": true,
"$touched": false,
"$untouched": true,
"$focused": false,
"$pending": false,
"$error": {
"required": true
}
},
"email": {
"$name": "email",
"$dirty": false,
"$pristine": true,
"$valid": false,
"$invalid": true,
"$touched": false,
"$untouched": true,
"$focused": false,
"$pending": false,
"$error": {
"email": true
}
}
}kenry-form
stateObject on which form state is settagString, defaults toformshow-messagesString, applies to all childfield-messages, show errors dependant on form field state e.g.$touched,$dirty || $touched, '$touched || $submitted'
validate
stateOptional way of passing in the form state. If omitted form state will be found in the $parentcustomObject containing one or many custom validators.{validatorName: validatorFunction}tagString which specifies what element tag should be rendered by thevalidatecomponent, defaults tospan
field-messages
stateOptional way of passing in the form state. If omitted form state will be found in the $parentnameString which specifies the related field nametagString, defaults todivshowString, show error dependant on form field state e.g.$touched,$dirty || $touched, '$touched || $submitted'auto-labelBoolean, defaults to false. Automatically set theforattribute of labels found inside thefield-messagescomponent
field
tagString, defaults todivauto-labelBoolean, defaults to true. Automatically setforandidattributes of label and input elements found inside thevalidatecomponent
Config
Set config options when using Vue.use(KenryForm, options), or when using a mixin mixins: [new KenryForm(options)] defaults:
- let options = {
- kMessages: {
required: 'system is required'- },
- validators: {
functions: {
}, isFieldColor: true, isAllowMessage: true'upper-character': function(value, attrValue, vnode) { return value === value.toLocaleUpperCase(); } }, messages: { 'upper-character': 'This field must be capital letter' }- };
{
validators: {},
formComponent: 'kenryForm',
formTag: 'form',
fieldComponent: 'field',
isFieldColor:true,
isAllowMessage:true,
Promise: typeof Promise === 'function' ? Promise : null
}