Skip to content

feat(Form): add validate on error-input #4336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/content/3.components/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ The Form component automatically triggers validation when an input emits an `inp
- Validation on `input` occurs **as you type**.
- Validation on `change` occurs when you **commit to a value**.
- Validation on `blur` happens when an input **loses focus**.
- Validation on `error-input` happens when as you type on an input with an error.

You can control when validation happens this using the `validate-on` prop.

Expand Down
2 changes: 1 addition & 1 deletion playground/app/pages/components/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const disabled = ref(false)
<div class="border border-default rounded-lg">
<div class="py-2 px-4 flex gap-4 items-center">
<UFormField label="Validate on" class="flex items-center gap-2">
<USelectMenu v-model="validateOn" :items="['input', 'change', 'blur']" multiple class="w-48" />
<USelectMenu v-model="validateOn" :items="['input', 'change', 'blur', 'error-input']" multiple class="w-48" />
</UFormField>
<UCheckbox v-model="disabled" label="Disabled" />
</div>
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/components/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { DeepReadonly } from 'vue'
import type { AppConfig } from '@nuxt/schema'
import theme from '#build/ui/form'
import type { FormSchema, FormError, FormInputEvents, FormErrorEvent, FormSubmitEvent, FormEvent, Form, FormErrorWithId, InferInput, InferOutput, FormData } from '../types/form'
import type { FormSchema, FormError, FormErrorEvent, FormSubmitEvent, FormEvent, Form, FormErrorWithId, InferInput, InferOutput, FormData, FormValidateOn } from '../types/form'
import type { ComponentConfig } from '../types/utils'

type FormConfig = ComponentConfig<typeof theme, AppConfig, 'form'>
Expand All @@ -23,7 +23,8 @@ export interface FormProps<S extends FormSchema, T extends boolean = true> {
* The list of input events that trigger the form validation.
* @defaultValue `['blur', 'change', 'input']`
*/
validateOn?: FormInputEvents[]
validateOn?: FormValidateOn[]

/** Disable all inputs inside the form. */
disabled?: boolean
/**
Expand Down Expand Up @@ -77,7 +78,7 @@ type O = InferOutput<S>

const props = withDefaults(defineProps<FormProps<S, T>>(), {
validateOn() {
return ['input', 'blur', 'change'] as FormInputEvents[]
return ['input', 'blur', 'change'] as FormValidateOn[]
},
validateOnInputDelay: 300,
attach: true,
Expand Down Expand Up @@ -110,12 +111,14 @@ onMounted(async () => {
nestedForms.value.set(event.formId, { validate: event.validate })
} else if (event.type === 'detach') {
nestedForms.value.delete(event.formId)
} else if (props.validateOn?.includes(event.type) && !loading.value) {
} else if (props.validateOn?.includes(event.type as FormValidateOn) && !loading.value) {
if (event.type !== 'input') {
await _validate({ name: event.name, silent: true, nested: false })
} else if (event.eager || blurredFields.has(event.name)) {
await _validate({ name: event.name, silent: true, nested: false })
}
} else if (props.validateOn?.includes('error-input') && errors.value?.find(e => e.name === event.name)) {
await _validate({ name: event.name, silent: true, nested: false })
}

if (event.type === 'blur') {
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export type FormData<S extends FormSchema, T extends boolean = true> = T extends

export type FormInputEvents = 'input' | 'blur' | 'change' | 'focus'

export type FormValidateOn = 'input' | 'blur' | 'change' | 'error-input'

export interface FormError<P extends string = string> {
name?: P
message: string
Expand Down
4 changes: 2 additions & 2 deletions test/components/Checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/checkbox'
import { renderForm } from '../utils/form'
import { mount, flushPromises } from '@vue/test-utils'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('Checkbox', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Checkbox', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/CheckboxGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import theme from '#build/ui/checkbox-group'
import themeCheckbox from '#build/ui/checkbox'
import { flushPromises, mount } from '@vue/test-utils'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('CheckboxGroup', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('CheckboxGroup', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
16 changes: 14 additions & 2 deletions test/components/Input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Input, { type InputProps, type InputSlots } from '../../src/runtime/compo
import ComponentRender from '../component-render'
import theme from '#build/ui/input'

import type { FormValidateOn } from '~/src/module'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'

describe('Input', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Input', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[], eagerValidation?: boolean) {
async function createForm(validateOn?: FormValidateOn[], eagerValidation?: boolean) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down Expand Up @@ -160,6 +160,18 @@ describe('Input', () => {
expect(wrapper.text()).not.toContain('Error message')
})

test('validate on error-input works', async () => {
const { input, wrapper } = await createForm(['error-input', 'blur'], true)
await input.setValue('value')
expect(wrapper.text()).not.toContain('Error message')

await input.trigger('blur')
expect(wrapper.text()).toContain('Error message')

await input.setValue('valid')
expect(wrapper.text()).not.toContain('Error message')
})

test('validate on input without eager validation works', async () => {
const { input, wrapper } = await createForm(['input'])

Expand Down
4 changes: 2 additions & 2 deletions test/components/InputMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/input'
import { renderForm } from '../utils/form'
import { flushPromises, mount } from '@vue/test-utils'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'
import { expectEmitPayloadType } from '../utils/types'

describe('InputMenu', () => {
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('InputMenu', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/InputNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { reactive } from 'vue'
import InputNumber, { type InputNumberProps, type InputNumberSlots } from '../../src/runtime/components/InputNumber.vue'
import ComponentRender from '../component-render'
import theme from '#build/ui/input-number'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'
import { renderForm } from '../utils/form'

describe('InputNumber', () => {
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('InputNumber', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
state: reactive({ value: 0 }),
props: {
Expand Down
4 changes: 2 additions & 2 deletions test/components/PinInput.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/pin-input'

import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('PinInput', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('PinInput', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/RadioGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/radio-group'
import { flushPromises, mount } from '@vue/test-utils'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('RadioGroup', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('RadioGroup', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/Select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Select, { type SelectProps, type SelectSlots } from '../../src/runtime/co
import ComponentRender from '../component-render'
import theme from '#build/ui/input'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'
import { expectEmitPayloadType } from '../utils/types'

describe('Select', () => {
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('Select', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/SelectMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/input'
import { renderForm } from '../utils/form'
import { flushPromises, mount } from '@vue/test-utils'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'
import { expectEmitPayloadType } from '../utils/types'

describe('SelectMenu', () => {
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('SelectMenu', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/Slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/slider'
import { flushPromises, mount } from '@vue/test-utils'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('Slider', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('Slider', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/Switch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentRender from '../component-render'
import theme from '#build/ui/switch'
import { flushPromises, mount } from '@vue/test-utils'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('Switch', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Switch', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
async function createForm(validateOn?: FormValidateOn[]) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down
4 changes: 2 additions & 2 deletions test/components/Textarea.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Textarea, { type TextareaProps, type TextareaSlots } from '../../src/runt
import ComponentRender from '../component-render'
import theme from '#build/ui/textarea'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
import type { FormValidateOn } from '~/src/module'

describe('Textarea', () => {
const sizes = Object.keys(theme.variants.size) as any
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('Textarea', () => {
})

describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[], eagerValidation?: boolean) {
async function createForm(validateOn?: FormValidateOn[], eagerValidation?: boolean) {
const wrapper = await renderForm({
props: {
validateOn,
Expand Down