Envolva qualquer componente de formulário com um FormField. Usado em um Form, ele oferece validação e tratamento de erros.
Use a prop label para definir o rótulo do controle de formulário.
<template>
<NFormField label="Email">
<NInput placeholder="Enter your email" />
</NFormField>
</template>
for do rótulo e o controle de formulário são associados por um id único, se não for fornecido.Ao usar a prop required, um asterisco é adicionado ao lado do rótulo.
<template>
<NFormField label="Email" required>
<NInput placeholder="Enter your email" />
</NFormField>
</template>
Use a prop description para fornecer informações adicionais abaixo do rótulo.
We'll never share your email with anyone else.
<template>
<NFormField label="Email" description="We'll never share your email with anyone else.">
<NInput placeholder="Enter your email" class="w-full" />
</NFormField>
</template>
Use a prop hint para exibir uma mensagem de dica ao lado do rótulo.
<template>
<NFormField label="Email" hint="Optional">
<NInput placeholder="Enter your email" />
</NFormField>
</template>
Use a prop help para exibir uma mensagem de ajuda abaixo do controle de formulário. Quando usada junto com a prop error, a prop error tem precedência.
<template>
<NFormField label="Email" help="Please enter a valid email address.">
<NInput placeholder="Enter your email" class="w-full" />
</NFormField>
</template>
Use a prop error para exibir uma mensagem de erro abaixo do controle de formulário. Quando usada junto com a prop help, a prop error tem precedência.
Quando usada dentro de um Form, isso é definido automaticamente quando ocorre um erro de validação.
<template>
<NFormField label="Email" error="Please enter a valid email address.">
<NInput placeholder="Enter your email" class="w-full" />
</NFormField>
</template>
color como error no controle de formulário. Você pode alterar isso globalmente no seu app.config.ts.Use a prop error-pattern para associar os erros do formulário a uma expressão regular. Isso é especialmente relevante para componentes com valores de array, como o InputTags, em que os erros incluem os índices do array no nome (ex.: tags.0).
Use a prop size para alterar o tamanho do FormField; o size é repassado ao controle de formulário.
We'll never share your email with anyone else.
<template>
<NFormField
label="Email"
description="We'll never share your email with anyone else."
hint="Optional"
help="Please enter a valid email address."
size="xl"
>
<NInput placeholder="Enter your email" class="w-full" />
</NFormField>
</template>
Use a prop orientation para alterar o layout do FormField. O padrão é vertical.
<template>
<NFormField
orientation="horizontal"
label="Email"
help="Please enter a valid email address."
class="w-72"
>
<NInput placeholder="Enter your email" class="w-full" />
</NFormField>
</template>
| Prop | Default | Type |
|---|---|---|
as | 'div' | anyThe element or component this component should render as. |
name | stringThe name of the FormField. Also used to match form errors. | |
errorPattern | RegExpA regular expression to match form error names. Useful for components with array values such as InputTags, where errors include array indices in their name (e.g. | |
label | string | |
description | string | |
help | string | |
error | undefined | string | false | true |
hint | string | |
size | 'md' | "md" | "xs" | "sm" | "lg" | "xl" |
required | boolean | |
eagerValidation | boolean If true, validation on input will be active immediately instead of waiting for a blur event. | |
validateOnInputDelay | `300` | numberDelay in milliseconds before validating the form on input events. |
orientation | 'vertical' | "vertical" | "horizontal"The orientation of the form field. |
ui | { root?: SlotClass; wrapper?: SlotClass; labelWrapper?: SlotClass; label?: SlotClass; container?: SlotClass; description?: SlotClass; error?: SlotClass; hint?: SlotClass; help?: SlotClass; } |
| Slot | Type |
|---|---|
label | { label: string | undefined; } |
hint | { hint: string | undefined; } |
description | { description: string | undefined; } |
help | { help: string | undefined; } |
error | { error: string | true | undefined; } |
default | { error: string | true | undefined; } |
export default defineAppConfig({
ui: {
formField: {
slots: {
root: '',
wrapper: '',
labelWrapper: 'flex content-center items-center justify-between gap-1',
label: 'block font-medium text-default',
container: 'relative',
description: 'text-muted',
error: 'mt-2 text-error',
hint: 'text-muted',
help: 'mt-2 text-muted'
},
variants: {
size: {
xs: {
root: 'text-xs'
},
sm: {
root: 'text-xs'
},
md: {
root: 'text-sm'
},
lg: {
root: 'text-sm'
},
xl: {
root: 'text-base'
}
},
required: {
true: {
label: "after:content-['*'] after:ms-0.5 after:text-error"
}
},
orientation: {
vertical: {
container: 'mt-1'
},
horizontal: {
root: 'flex justify-between place-items-baseline gap-2'
}
}
},
defaultVariants: {
size: 'md',
orientation: 'vertical'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nitro/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
formField: {
slots: {
root: '',
wrapper: '',
labelWrapper: 'flex content-center items-center justify-between gap-1',
label: 'block font-medium text-default',
container: 'relative',
description: 'text-muted',
error: 'mt-2 text-error',
hint: 'text-muted',
help: 'mt-2 text-muted'
},
variants: {
size: {
xs: {
root: 'text-xs'
},
sm: {
root: 'text-xs'
},
md: {
root: 'text-sm'
},
lg: {
root: 'text-sm'
},
xl: {
root: 'text-base'
}
},
required: {
true: {
label: "after:content-['*'] after:ms-0.5 after:text-error"
}
},
orientation: {
vertical: {
container: 'mt-1'
},
horizontal: {
root: 'flex justify-between place-items-baseline gap-2'
}
}
},
defaultVariants: {
size: 'md',
orientation: 'vertical'
}
}
}
})
]
})