Appearance
Valibot
Guides
Nullish/Optional
Nullish/Optional
In some cases, when the form is reset, the validation error will trigger even if the field is not required. To avoid this, you can use v.nullish() to set the field to null when the form is reset.
js
start_date: v.nullish(
v.optional(
v.pipe(
v.string(),
v.check(val => dayjs(val).isValid(), 'Invalid date format')
)
)
)
resetForm({
values: {
start_date: null,
},
}, { force: true });Custom error message for all validations
Custom error message for all validations
This message is a log from the Valibot Discord channel (https://discord.com/channels/1252985447273992222/1252985448025030741/1327601871879143424)
<@redacted_username>
Hi again, Is it possible to return a custom error message for all validations of a schema that don't already have a custom error message, without writing it on each of the validations?
Something like:
typescript
const schema = v.object({
name: v.pipe(v.string("Please provide a name"), v.nonEmpty("Please provide a name"), v.maxLength(40, "Name is too long..."))
...
})Be written as:
typescript
const schema = v.object({
name: customMsg(v.pipe(v.string(), v.nonEmpty(), v.maxLength(40, "Name is too long...")), "Please provide a name")
...
})<@redacted_username>
Hey ! Yes, check out this solution and the config doc
<@redacted_username> here is the solution:
ts
import * as v from 'valibot';
const Schema = v.config(
v.pipe(
v.string('Specific error.'),
v.transform(Number),
v.integer(),
v.minValue(0)
),
{ message: "Error message triggers if specific message isn't available." }
);