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

Formik + Yup + ReactQuill: Error message doesn't display #1798

Open
lucassardois opened this issue Sep 4, 2019 · 2 comments
Open

Formik + Yup + ReactQuill: Error message doesn't display #1798

lucassardois opened this issue Sep 4, 2019 · 2 comments
Labels

Comments

@lucassardois
Copy link

Hello,

I have a problem with Formik, Yup and ReactQuill: I have the following code with my ReactQuill component working but for some reason errors messages send by Yup are never shown, like if ReactQuill and Formik value aren't synced. When I submit the form I correctly see the ReactQuill values trough.

Yup schema:

const schema = Yup.object().shape({
  long_description: Yup.string()
    .required()
})

Formik & ReactQuill:

<Formik
    initialValues={{
    name: '',
    image: '',
    short_description: '',
    long_description: '',
    latitude: '',
    longitude: '',
    categories: []
    }}
    validationSchema={schema}
    onSubmit={values => {
    console.log(values)
    }}
>
    {({
    handleSubmit,
    handleChange,
    handleBlur,
    values,
    errors,
    touched
    }) => (
        <Form noValidate onSubmit={handleSubmit}>
            <Form.Group>
            <Form.Label>Longue description</Form.Label>
            <Field name='long_description'>
                {({ field }) =>
                <ReactQuill
                    defaultValue={field.value}
                    value={field.value}
                    onChange={field.onChange(field.name)} 
                    onBlur={field.onBlur(field.name)}
                    rows="5"
                    modules={{ toolbar: toolbarOptions }}
                />
                }
            </Field>
            <Form.Control.Feedback type="invalid">
                <ErrorMessage name='long_description' />
            </Form.Control.Feedback>
            </Form.Group>   <Form noValidate onSubmit={handleSubmit}>
        </Form>
    )
</Formik>
@stale stale bot added the stale label Nov 3, 2019
@quentingirard
Copy link

quentingirard commented Mar 27, 2020

You have to set message in required

const schema = Yup.object().shape({long_description: Yup.string().required("My message")})

@stale stale bot removed the stale label Mar 27, 2020
@stale stale bot added the stale label May 30, 2020
@mrvisser
Copy link

mrvisser commented Jul 28, 2021

I think you'd want something a little more sophisticated than required(), because anything that isn't a vanilla field will almost always at least have an empty paragraph tag in it as a string.

A custom validator using addMethod is probably better, something that validates like this:

const content = new DOMParser().parseFromString(value, 'text/html')?.body.textContent?.trim() ?? '';
if (content === '') {
  // Field is empty
} else {
  // Field has text content
}

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

No branches or pull requests

3 participants