Last active
March 9, 2021 00:18
-
-
Save ramadimasatria/1819d4da13afb2ec3c2505f88bb760af to your computer and use it in GitHub Desktop.
VeeValidate minimal with custom locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue' | |
import VeeValidate from 'vee-validate/dist/vee-validate.minimal.esm' | |
import id from './locale-id' | |
Vue.use(VeeValidate) | |
const rulesPlugin = ({ Validator }) => { | |
Validator.localize('id', id); | |
}; | |
VeeValidate.use(rulesPlugin); | |
// .... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatFileSize = (size) => { | |
const units = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
const threshold = 1024; | |
size = Number(size) * threshold; | |
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(threshold)); | |
return `${(size / Math.pow(threshold, i)).toFixed(2) * 1} ${units[i]}`; | |
}; | |
const messages = { | |
_default: (field) => `Nilai ${field} tidak sah.`, | |
after: (field, [target]) => `${field} harus sebelum ${target}.`, | |
alpha_dash: (field) => `${field} boleh mengandung karakter alfanumerik, tanda hubung, dan garis bawah.`, | |
alpha_num: (field) => `${field} hanya boleh mengandung karakter alfanumerik.`, | |
alpha_spaces: (field) => `${field} hanya boleh berisi karakter alfabet serta spasi.`, | |
alpha: (field) => `${field} hanya boleh mengandung karakter alfabet.`, | |
before: (field, [target]) => `${field} harus setelah ${target}.`, | |
between: (field, [min, max]) => `${field} harus di antara ${min} dan ${max}.`, | |
confirmed: (field, [confirmedField]) => `${field} tidak cocok dengan ${confirmedField}.`, | |
credit_card: (field) => `${field} tidak sah.`, | |
date_between: (field, [min, max]) => `${field} harus di antara ${min} dan ${max}.`, | |
date_format: (field, [format]) => `${field} harus dalam format ${format}.`, | |
decimal: (field, [decimals = '*'] = []) => `${field} harus berupa angka dan boleh mengandung ${decimals === '*' ? '' : decimals} titik desimal.`, | |
digits: (field, [length]) => `${field} harus berupa ${length} digit angka.`, | |
dimensions: (field, [width, height]) => `${field} harus berdimensi lebar ${width} pixel dan tinggi ${height} pixel.`, | |
email: (field) => `${field} harus berupa alamat surel yang benar.`, | |
ext: (field) => `${field} harus berupa berkas yang benar.`, | |
image: (field) => `${field} harus berupa gambar.`, | |
in: (field) => `${field} harus berupa nilai yang sah.`, | |
integer: (field) => `${field} harus berupa bilangan bulat.`, | |
ip: (field) => `${field} harus berupa alamat ip yang sah.`, | |
length: (field, [length, max]) => { | |
if (max) { | |
return `Panjang ${field} harus di antara ${length} dan ${max}.`; | |
} | |
return `Panjang ${field} harus tepat ${length}.`; | |
}, | |
max: (field, [length]) => `${field} tidak boleh lebih dari ${length} karakter.`, | |
max_value: (field, [size]) => `Nilai ${field} tidak boleh lebih dari ${size}.`, | |
mimes: (field) => `Tipe berkas ${field} harus benar.`, | |
min: (field, [length]) => `${field} minimal mengandung ${length} karakter.`, | |
min_value: (field, [size]) => `Nilai ${field} tidak boleh kurang dari ${size}.`, | |
not_in: (field) => `${field} harus berupa nilai yang sah.`, | |
numeric: (field) => `${field} harus berupa angka.`, | |
regex: (field) => `Format ${field} salah.`, | |
required: (field) => `${field} harus diisi.`, | |
size: (field, [size]) => `${field} harus lebih kecil dari ${formatFileSize(size)}.`, | |
url: (field) => `${field} harus berupa tautan yang benar.` | |
}; | |
const locale = { | |
name: 'id', | |
messages, | |
attributes: {} | |
}; | |
export default locale; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment