-
-
Notifications
You must be signed in to change notification settings 8000 - Fork 215
Problem triggering form validations without calling submit. #398
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
Comments
@omerman what are you trying to accomplish with this? if you're using the |
Actually I have validations on each field of the form.. I would like to validate them in any given time without calling the submit to do so.. Do you have an idea how to approach it? |
are you using react-final-form? if so, each |
@Dakkers I don't think it is relevant where I use final-form.. I think its trivial to have the ability to trigger a validation at any time using the form api object. And as of now I think this ability is missing, and I must say Im surprised this issue has not been raised before :P |
function Example () {
let { form, dirty, invalid } = useForm({
onSubmit () { },
validate () {
return { }
},
initialValues: {
username: '',
password: ''
}
})
const username = useField('username', form)
const password = useField('password', form)
return <>
<Form.Item label='username' required {...get_field_item(username.meta)}>
<Input size='l' {...username.input} />
</Form.Item>
<Form.Item label='password' required {...get_field_item(password.meta)}>
<Input size='l' {...password.input} />
</Form.Item>
<button onClick={() => {
if (dirty || invalid) {
form.batch(() => {
[username, password].forEach( field => {
field.input.onFocus()
field.input.onBlur()
})
})
return
}
}}>validate</button>
</>
}
function get_field_item ({ touched, error }: { touched?: boolean, error?: any }): {
status: 'success' | 'error' | 'validating'
message: React.ReactNode
} {
if (!touched) return { status: undefined, message: '' }
if (error) return { status: 'error', message: error }
return { status: 'success', message: '' }
} |
@ShenHongFei Thank you for the workaround, though I was hoping for a method as part of the api like ->
|
Absolutely! We need the ability to trigger validation manually in FormApi. const errors = await form.validate()
console.log(errors.username) |
@omerman the validations should happen as you change the field. |
U can just add a mutator called like validate const form = createForm({
...
mutators: {
validate: () => {}
}
}) and called it when u need form.mutators.validate() |
I think its trivial to have a way to trigger form validations manually without calling submit.
Is there a way to achieve this?
Thanks you rock!
The text was updated successfully, but these errors were encountered: