Skip to content

Commit

Permalink
feat: custom validator message
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanvier committed Feb 7, 2024
1 parent 943a102 commit 35b88ef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/guide/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ Nuxt-form comes with a built-in field validation system. It utilizes [class-vali
/>
```

##### Custom validator message
```VUE
<FormInput
name="fieldname"
:rules="['isEmail', {minLength: [3], message: 'custom' }]"
/>
```


## 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:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
name="asdf"
:rules="[ 'isEmail']"
/>
<FormInput label="" type="checkbox" name="tosnlpd" :rules="[{ equals: [true], message: 'coucou' }]" />
<FormInput name="safs" :rules="['isNotEmpty']" />
<FormSubmit>
Submit
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/composables/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
Expand All @@ -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)
}
})

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils/validators/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 35b88ef

Please sign in to comment.