mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-25 09:20:36 +01:00
fix(Form)!: resolve async validation in yup & issue directly mutate state (#2701)
This commit is contained in:
@@ -60,6 +60,8 @@ export default defineComponent({
|
|||||||
const formId = useId()
|
const formId = useId()
|
||||||
const bus = useEventBus<FormEvent>(`form-${formId}`)
|
const bus = useEventBus<FormEvent>(`form-${formId}`)
|
||||||
|
|
||||||
|
const parsedValue = ref(null)
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
bus.on(async (event) => {
|
bus.on(async (event) => {
|
||||||
if (event.type !== 'submit' && props.validateOn?.includes(event.type)) {
|
if (event.type !== 'submit' && props.validateOn?.includes(event.type)) {
|
||||||
@@ -87,7 +89,7 @@ export default defineComponent({
|
|||||||
if (errors) {
|
if (errors) {
|
||||||
errs = errs.concat(errors)
|
errs = errs.concat(errors)
|
||||||
} else {
|
} else {
|
||||||
Object.assign(props.state, result)
|
parsedValue.value = result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +132,7 @@ export default defineComponent({
|
|||||||
if (props.validateOn?.includes('submit')) {
|
if (props.validateOn?.includes('submit')) {
|
||||||
await validate()
|
await validate()
|
||||||
}
|
}
|
||||||
event.data = props.state
|
event.data = props.schema ? parsedValue.value : props.state
|
||||||
emit('submit', event)
|
emit('submit', event)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!(error instanceof FormException)) {
|
if (!(error instanceof FormException)) {
|
||||||
@@ -321,7 +323,7 @@ async function validateYupSchema(
|
|||||||
schema: YupObjectSchema<any>
|
schema: YupObjectSchema<any>
|
||||||
): Promise<ValidateReturnSchema<typeof state>> {
|
): Promise<ValidateReturnSchema<typeof state>> {
|
||||||
try {
|
try {
|
||||||
const result = schema.validateSync(state, { abortEarly: false })
|
const result = await schema.validate(state, { abortEarly: false })
|
||||||
return {
|
return {
|
||||||
errors: null,
|
errors: null,
|
||||||
result
|
result
|
||||||
|
|||||||
Reference in New Issue
Block a user