Formik: Changing single input value as a user makes all inputs dirty (setting errors messages)

Created on 7 Jul 2017  路  5Comments  路  Source: formium/formik

Most helpful comment

Be sure to add onBlur={handleBlur}, so that touched will get set when you lose focus on the input.

All 5 comments

+1 here.

Ideally, I'd like for handleChange to only validate and return errors for the field that's been changed. I'd then like all the form's values to be validated and any errors returned before handleSubmit is called.

To ensure you don't see error messages displayed on untouched fields, you want to check the touched value for that field when deciding whether to display the error message or not.

For example:

{errors.email && touched.email && <div>{errors.email}</div>}

@jaredpalmer I think it'd be good if we updated the examples to show the proper use of touched.

Oh rad, thanks @eonwhite!

touched helper is not working properly, or i am using it wrong? Touched is not returning me anything.

const MyForm = ({ values, touched, handleChange, handleSubmit, errors })
...
<input id="nome" placeholder="Enter your nome" type="text" value={values.nome} onChange={handleChange}/>
{errors.nome && touched.nome && <div  style={{ color: 'red' }}>{errors.nome}</div>}

live example https://codesandbox.io/s/wpMx40WBX

Be sure to add onBlur={handleBlur}, so that touched will get set when you lose focus on the input.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

najisawas picture najisawas  路  3Comments

outaTiME picture outaTiME  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

emartini picture emartini  路  3Comments