diff --git a/docs/guide/validation.md b/docs/guide/validation.md index 9275043..addf9a8 100644 --- a/docs/guide/validation.md +++ b/docs/guide/validation.md @@ -10,6 +10,15 @@ Nuxt-form comes with a built-in field validation system. It utilizes [class-vali /> ``` +##### Custom validator message +```VUE + +``` + + ## Triggering Validation While some validations are triggered on input changes, others need to be activated by a validation method. There are two ways to trigger this: diff --git a/package.json b/package.json index b75ec71..293e309 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@wgr-sa/nuxt-form", "description": "Form builder for Nuxt", - "version": "0.8.0", + "version": "0.8.1", "repository": "https://github.com/WGR-SA/nuxt-form.git", "author": "jeanvier", "license": "MIT", diff --git a/playground/app.vue b/playground/app.vue index 50f4109..073b4fd 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -16,6 +16,7 @@ name="asdf" :rules="[ 'isEmail']" /> + Submit diff --git a/src/runtime/composables/validator.ts b/src/runtime/composables/validator.ts index 89a1c9c..04332a1 100644 --- a/src/runtime/composables/validator.ts +++ b/src/runtime/composables/validator.ts @@ -12,7 +12,13 @@ export const useFormValidator = () => { return [] } - form.validator.rules[field].forEach((rule: any) => { + console.log(form.validator.rules); + + + form.validator.rules[field].forEach((rule: any) => { + + console.log(rule); + const validator = validators[rule.$params.type as keyof typeof validators] const type_rules = [ 'isEmail', 'isNumber' ] @@ -24,8 +30,8 @@ export const useFormValidator = () => { // @ts-ignore TODO: import only validation functions const result = validator(form.data.state[field], rule.$params.options) - if (!result && (form.data.state[field].length > 0 || ['error', 'validate'].includes(form.state.status))) { - errors.value.push(rule.$message) + if (!result && (form.data.state[field].length > 0 && form.data.state[field] !== 'false' || ['error', 'validate'].includes(form.state.status))) { + errors.value.push(rule.custom_message ?? rule.$message) } }) diff --git a/src/runtime/utils/validators/validator.ts b/src/runtime/utils/validators/validator.ts index f68a6cd..620a855 100644 --- a/src/runtime/utils/validators/validator.ts +++ b/src/runtime/utils/validators/validator.ts @@ -9,7 +9,7 @@ export class FormValidator { return { $params: { type: r }, $message: r } } const rule = Object.keys(r)[0] - return { $params: { type: rule, options: [...r[rule]] }, $message: r } + return { $params: { type: rule, options: [...r[rule]] }, $message: r, custom_message: r.message } }) this.updateValidatorMessages(options.messages)