vue-validator-util v0.3.2
vue-validator-util
Vue.js mixin with utility methods for vue-validator. Useful when using dynamic fields.
Install
npm install vue-validator-utilUsage
Within some Vue.js component:
JavaScript (ES6)
import Validation from 'vue-validator-util'
export default {
data () {
return {
fields: [{
name: 'some-field-name',
label: 'My field label',
value: null,
validate: {
required: {
rule: true,
message: 'The field is required'
}
}
}]
}
},
mixins: [
Validation
]
}Template
<validator name="some-validator-name">
<div v-for="field in fields"
class="form-group"
:class="{ 'has-error': hasValidationError(field.name, 'some-validator-name') }">
<label :for="field.name">{{ field.label }}</label>
<input type="text" class="form-control"
:id="field.name"
:field="field.name"
v-model="field.value"
v-validate="field.validate" />
<p class="help-block" v-for="msg in validationMessages(field.name, 'some-validator-name')">
{{ msg }}
</p>
</div>
</validator>Methods
getValidator(name)
Returns a vue-validator instance.
- the default
namevalue is$validation - the
nameis automatically transformed tocamelCaseand is also getting a$prefix, so you can passsome-validator-namewhich will be translated to$someValidatorName
getValidatorField(fieldName, validatorName)
Returns a vue-validator field.
- the
fieldNameis automatically transformed tocamelCase, so passingsome-validator-field-namewill get translated tosomeValidatorFieldName - the
validatorNameis passed togetValidator
hasValidationError(fieldName, validatorName)
Check whether a vue-validator field is invalid. The field is considered invalid only when it's
dirtyandinvalidproperties are bothtrue.
- the
fieldNameis passed togetValidatorField - the
validatorNameis passed togetValidator
validationMessages(fieldName, validatorName)
Returns
messagesfor a vue-validator field.
- the
fieldNameis passed togetValidatorField - the
validatorNameis passed togetValidator
firstValidationMessage(fieldName, validatorName)
Returns the first message for a vue-validator field. Useful when trying to display a single message at a time.
- the
fieldNameis passed togetValidatorField - the
validatorNameis passed togetValidator
Example
Assuming we have a field which is invalid for more than one rule (i.e.
requiredandminlength), the following example will first displayThe field is requiredand after therequiredrule passes, it will displayThe field must have at least 10 characters..
data () {
return {
value: null,
validate: {
required: {
rule: true,
message: 'The field is required.'
},
minlength: {
rule: 10,
message: 'The field must have at least 10 characters.'
}
}
}
}Template:
<validator name="validation">
<div class="form-group"
:class="{ 'has-error': hasValidationError('some-field-name') }">
<label for="field-1">My label</label>
<input type="text" class="form-control"
id="field-1"
field="some-field-name"
v-model="value"
v-validate="validate" />
<p class="help-block" v-if="hasValidationError('some-field-name')">
{{ firstValidationMessage('some-field-name') }}
</p>
</div>
</validator>--
Build
You can easily build vue-validator-util yourself.
Prerequisites
Our build tool of choice is Webpack. You should have webpack installed globally:
npm install -g webpackAnd the build dependencies:
npm installBuild
Any of the following will create the file dist/vue-validator-util.js.
Production build
Includes minification and several optimizations:
npm run buildDevelopment build
A faster build suited for development, with no optimizations and without minification:
npm run build-devWatch
Start an initial development build and then FAST continuous incremental builds:
npm run dev