Skip to content

Commit

Permalink
fix: date validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanvier committed Nov 19, 2024
1 parent f013956 commit 9ebb40b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
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.9.4",
"version": "0.9.5",
"repository": "https://github.com/WGR-SA/nuxt-form.git",
"author": "jeanvier",
"license": "MIT",
Expand Down
21 changes: 5 additions & 16 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
// watchEffect(() => {
// if testForm.value.isReady()
// testForm.value.submit()
import { max } from 'class-validator';
// })
</script>

<template>
Expand All @@ -15,21 +13,12 @@ import { max } from 'class-validator';
ref="testForm"
action="https://httpbin.org/post"
>
<!-- <FormInput
<FormInput
name="asdf"
:rules="[ 'isEmail']"
/> -->
<FormSelect
name="yo"
:options="{ 'O': 'Oui', 'N': 'Non'}"
:rules="['isNotEmpty']"
empty="-- Select an option --"
type="date"
:rules="[ {minDate: [new Date()]}]"
/>
<!-- <FormInput label='yo <a href="#">ok</a>' type="checkbox" name="tosnlpd" :rules="[{ equals: [true], message: 'coucou' }]" />
<FormInput name="yo" type="number" :rules="['isNotEmpty', {min: [1]}, {max: [25]}]" /> -->
<!-- <FormTextarea name="saffs" :rules="['isNotEmpty']" /> -->
<!-- <FormRadio :options="{'O': 'Oui', 'N': 'Non'}" name="yo" laebl="Yo" />
<FormRadio :options="{'O': 'Oui', 'N': 'Non'}" name="yos" label="Yos" :rules="['isNotEmpty']" :empty="true" /> -->

<FormSubmit>
Submit
</FormSubmit>
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/composables/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ref } from 'vue'
import * as validators from 'class-validator'
import { Form } from '#imports'

Expand All @@ -21,7 +20,7 @@ export const useFormValidator = () => {
switch (expectedType) {
case 'number': return Number(value)
case 'boolean': return value.toLowerCase() === 'true'
case 'date': return new Date(value)
case 'date': return value !== '' ? new Date(value) : null
case 'array': return tryParseJSON(value) ?? value.split(',').map(item => item.trim())
default: return value
}
Expand Down Expand Up @@ -57,6 +56,12 @@ export const useFormValidator = () => {
const expectedType = getExpectedType(validatorName)
const convertedValue = convertValue(fieldValue, expectedType)

if (expectedType === 'date') {
if (!(rule.$params.options[0] instanceof Date)) {
rule.$params.options[0] = new Date(rule.$params.options[0])
}
}

if (!validator(convertedValue, ...(rule.$params.options || [])) &&
(fieldValue.length > 0 && fieldValue !== 'false' || ['error', 'validate'].includes(form.state.status))) {
errors.push(rule.custom_message ?? rule.$message)
Expand Down
12 changes: 8 additions & 4 deletions src/runtime/utils/validators/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ export class FormValidator {
rule.$message = messages.get(rule.$params.type, 'validators') ?? rule.$message

if (rule.$params.options) {
if (['minDate', 'maxDate'].includes(rule.$params.type)) {
rule.$params.options[0] = new Date().toLocaleString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit' }).replace(',', '')
}
Object.keys(rule.$params.options).map((param) => rule.$message = rule.$message.replace(`{${param}}`, rule.$params.options[param]))

Object.keys(rule.$params.options).map((param) => {
let validationLabelValue = rule.$params.options[param]
if (['minDate', 'maxDate'].includes(rule.$params.type)) {
validationLabelValue = new Date(rule.$params.options[param]).toLocaleString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(',', '')
}
rule.$message = rule.$message.replace(`{${param}}`, validationLabelValue)
})
}
})
})
Expand Down

0 comments on commit 9ebb40b

Please sign in to comment.