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

Problem triggering form validations without calling submit. #398

Open
omerman opened this issue Dec 7, 2020 · 9 comments
Open

Problem triggering form validations without calling submit. #398

omerman opened this issue Dec 7, 2020 · 9 comments

Comments

@omerman
Copy link

omerman commented Dec 7, 2020

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!

@Dakkers
Copy link

Dakkers commented Dec 10, 2020

@omerman what are you trying to accomplish with this? if you're using the validate method that's passed into the Form instance then you could call that separately

@omerman
Copy link
Author

omerman commented Dec 11, 2020

@omerman what are you trying to accomplish with this? if you're using the validate method that's passed into the Form instance then you could call that separately

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?

@Dakkers
Copy link

Dakkers commented Dec 12, 2020

are you using react-final-form? if so, each Field has a validate prop. if this is a function you define inside your class then you could cal it form anywhere. if you're not using react-final-form, I'm not sure what the right approach is here.

@omerman
Copy link
Author

omerman commented Dec 12, 2020

@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

@ShenHongFei
Copy link

@omerman

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: '' }
}

@omerman
Copy link
Author

omerman commented Aug 25, 2021

@ShenHongFei Thank you for the workaround, though I was hoping for a method as part of the api like ->

async myFunc() {
const isFormValid = await form.triggerValidations();
console.log("is form valid?", isFormValid);
}

@ShenHongFei
Copy link

Absolutely! We need the ability to trigger validation manually in FormApi.

const errors = await form.validate()
console.log(errors.username)

@gertdreyer
Copy link
Collaborator

@binjospookie
Copy link

U can just add a mutator called like validate

const form = createForm({
...
mutators: {
   validate: () => {}
 }
})

and called it when u need

form.mutators.validate()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants