@privyid/vuelidate-penak
v1.0.0
Published
Vuelidate Penak is a tool that makes it even easier to display error messages in your Vue.js forms when using the Vuelidate library. With Vuelidate Penak, you can quickly and easily create and customize error messages for your form inputs, without having
Downloads
3
Keywords
Readme
Vuelidate Penak 🔥
Vuelidate Penak is a tool that makes it even easier to display error messages in your Vue.js forms when using the Vuelidate library. With Vuelidate Penak, you can quickly and easily create and customize error messages for your form inputs, without having to write any complex validation logic yourself. This makes it a great option for developers who want to add powerful form validation to their Vue.js applications, without having to spend a lot of time and effort on the implementation. Overall, Vuelidate Penak makes error message display from Vuelidate a breeze, allowing you to quickly and easily create user-friendly forms that are easy to use and maintain.
Table Of Content 📖
Installation 🚀
npm i @privyid/vuelidate-penak
Usage ✨
Global Usage
import VuelidatePenak from "@privyid/vuelidate-penak";
import Vue from "vue";
Vue.use(VuelidatePenak);
In your form component
<template>
<form @submit.prevent="handleSubmit">
<vuelidate-penak :validator="$v.form.username" field="Username">
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
<vuelidate-penak :validator="$v.form.password" field="Password">
<div slot-scope="{ errors, state }">
<p class="form--label">
Password
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
type="password"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</form>
</template>
<script>
import { required, minLength } from "vuelidate/lib/validators"
export default {
validations: {
form: {
username: { required },
password: { required, minLength: minLength(8) }
}
}
data() {
return {
form: {
username: '',
password: ''
}
}
},
methods: {
handleSubmit() {
this.$v.form.$touch()
if(this.$v.form.$invalid) {
return false
}
alert('Form Submitted')
}
}
}
</script>
Local Usage
<template>
<vuelidate-penak :validator="$v.form.username" field="Username">
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</template>
<script>
import { VuelidatePenak } from "@privyid/vuelidate-penak";
import { required } from "vuelidate/lib/validators";
export default {
components: {
VuelidatePenak,
},
validations: {
form: {
username: { required },
},
},
data() {
return {
form: {
username: "",
},
};
},
};
</script>
Custom Component
You can use component v-input from vuetify for show error messages
<template>
<validator-form :validator="$v.form.balance_types" field="Custom Component">
<div slot-scope="{ errors, state, invalid }">
<p class="form--label">
Balance Type
<small class="red--text">*</small>
</p>
<your-custom-component
v-model="state.$model"
:class="{error--input: invalid}"
></your-custom-component>
<v-input :error-messages="errors"></v-input>
</div>
</validator-form>
</template>
or if your custom component not use v-model to change the value
<template>
<validator-form :validator="$v.form.balance_types" field="Custom Component">
<div slot-scope="{ errors, state, invalid }">
<p class="form--label">
Balance Type
<small class="red--text">*</small>
</p>
<your-custom-component
:value="state.$model"
:class="{error--input: invalid}"
@change="state.$model = $event"
>
</your-custom-component>
<v-input :error-messages="errors"></v-input>
</div>
</validator-form>
</template>
Custom Message
Global Setting
import VuelidatePenak from "@privyid/vuelidate-penak";
import Vue from "vue";
Vue.use(VuelidatePenak, {
messages: {
required: "{{field}} tidak boleh kosong",
},
});
Spesific Input Form
<template>
<vuelidate-penak
:validator="$v.form.username"
field="Username"
:messages="{
required: "{{field}} tidak boleh kosong"
}"
>
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</template>
You can also pass the params from vuelidate $params See here.
Or you can create your custom $params See here
<template>
<vuelidate-penak
:validator="$v.form.no_hp"
field="No. Hp"
:messages="{
minLength: "{{field}} tidak boleh kurang dari {{min}}"
}"
>
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</template>
<script>
import { minLength } from "vuelidate/lib/validators"
export default {
validations: {
form: {
no_hp: { minLength: minLength(8) }
}
}
data() {
return {
form: {
username: '',
password: '',
}
}
},
methods: {
handleSubmit() {
this.$v.form.$touch()
if(this.$v.form.$invalid) {
return false
}
alert('Form Submitted')
}
}
}
</script>
Custom Validations
Vuelidate Penak not handle custom validation, because this package just help you to show the error message. So if you want to custom validation you just setting the custom validation at vuelidate. You can see here to more example for custom validation vuelidate Documentation Vuelidate
<template>
<form @submit.prevent="handleSubmit">
<vuelidate-penak
:validator="$v.form.username"
field="Username"
:messages="{ mustContainBudi: '{{field}} harus mengandung kata budi' }"
>
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
<vuelidate-penak :validator="$v.form.password" field="Password">
<div slot-scope="{ errors, state }">
<p class="form--label">
Password
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
type="password"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</form>
</template>
<script>
import { required, minLength } from "vuelidate/lib/validators"
export default {
validations: {
form: {
username: { required, mustContainBudi: (value) => String(value).toLowerCase().includes('budi') },
password: { required, minLength: minLength(8) }
}
}
data() {
return {
form: {
username: '',
password: ''
}
}
},
methods: {
handleSubmit() {
this.$v.form.$touch()
if(this.$v.form.$invalid) {
return false
}
alert('Form Submitted')
}
}
}
</script>
or you can setting the message in global
import VuelidatePenak from "@privyid/vuelidate-penak";
import Vue from "vue";
Vue.use(VuelidatePenak, {
messages: {
mustContainBudi: "{{field}} harus mengandung kata budi",
},
});
Props
| Props Name | Type | Default | Description |
| :---------- | :------- | :------------- | :-------------------------------------------------------------------- |
| validator
| object
| -
| Required. The validator from vuelidate |
| field
| string
| This Field
| The field name for your error message |
| messages
| object
| empty object
| Custom Error Message |
Slots
| Slot Name | Slot Scope | Description |
| :-------- | :------------------------------------------- | :--------------------------------- |
| default
| object<ValidationsType> | Required. The default Vue slot |
Validations Type
{
state: object,
invalid: boolean,
errors: string[]
}
Important Notes
The state
in slot scope is base on validator from vuelidate so technically this is props.
So if you strict with rule props cannot change directly, you can use state form
or $v.form
instead.
instead use this
<template>
<vuelidate-penak
:validator="$v.form.no_hp"
field="No. Hp"
>
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="state.$model"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</template>
You can use this
<template>
<vuelidate-penak
:validator="$v.form.no_hp"
field="No. Hp"
>
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="$v.form.no_hp"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
// or this
<vuelidate-penak
:validator="$v.form.no_hp"
field="No. Hp"
>
<div slot-scope="{ errors, state }">
<p class="form--label">
Username
<small class="red--text">*</small>
</p>
<v-text-field
v-model="form.no_hp"
dense
outlined
:error-messages="errors"
></v-text-field>
</div>
</vuelidate-penak>
</template>
TODO
- Create example codesandbox
- Create component for Vue 3 & Vuelidate Next
- Update Docs for Vue 3 & Vuelidate Next