Skip to content
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

fix(Form)!: resolve async validation in yup & issue directly mutate state #2701

Merged
merged 2 commits into from
Nov 23, 2024
Merged
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
8 changes: 5 additions & 3 deletions src/runtime/components/forms/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default defineComponent({
const formId = useId()
const bus = useEventBus<FormEvent>(`form-${formId}`)

const parsedValue = ref(null)

onMounted(() => {
bus.on(async (event) => {
if (event.type !== 'submit' && props.validateOn?.includes(event.type)) {
Expand Down Expand Up @@ -87,7 +89,7 @@ export default defineComponent({
if (errors) {
errs = errs.concat(errors)
} else {
Object.assign(props.state, result)
romhml marked this conversation as resolved.
Show resolved Hide resolved
parsedValue.value = result
}
}

Expand Down Expand Up @@ -130,7 +132,7 @@ export default defineComponent({
if (props.validateOn?.includes('submit')) {
await validate()
}
event.data = props.state
event.data = props.schema ? parsedValue.value : props.state
emit('submit', event)
} catch (error) {
if (!(error instanceof FormException)) {
Expand Down Expand Up @@ -321,7 +323,7 @@ async function validateYupSchema(
schema: YupObjectSchema<any>
): Promise<ValidateReturnSchema<typeof state>> {
try {
const result = schema.validateSync(state, { abortEarly: false })
const result = await schema.validate(state, { abortEarly: false })
return {
errors: null,
result
Expand Down