jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.97k stars 2.79k forks source link

I got error when I used "validateField" #1812

Open al-ani opened 5 years ago

al-ani commented 5 years ago

🐛 Bug report

I am trying to use validateField function however I got an error. I am just trying to use the basic example provided by Formik web site.

My code is

import React from 'react'; import { Formik } from 'formik'; const Basic = () => (

{ let errors = {}; if (!values.email) { errors.email = 'Required'; } else if ( !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email) ) { errors.email = 'Invalid email address'; } return errors; }} onSubmit={(values, { setSubmitting }) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); }} > {({ values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, validateField /* and other goodies */ }) => (
{errors.email && touched.email && errors.email} {errors.password && touched.password && errors.password} #here is the button that should check the
)}

);

export default Basic;

Current Behavior

When pressing on click, I got the following Error

react-dom.development.js:57 Uncaught Invariant Violation: Objects are not valid as a React child (found: TypeError: Cannot read property 'props' of undefined). If you meant to render a collection of children, use an array instead. in form (at Form.jsx:38) in Formik (at Form.jsx:7) in div (at Form.jsx:5) in Basic (at App.js:18) in Provider (at App.js:17) in App (at src/index.js:42) in ApolloProvider (at src/index.js:41) at invariant (http://localhost:3001/static/js/0.chunk.js:25383:19) at throwOnInvalidObjectType (http://localhost:3001/static/js/0.chunk.js:37612:9) at updateSlot (http://localhost:3001/static/js/0.chunk.js:37898:11) at reconcileChildrenArray (http://localhost:3001/static/js/0.chunk.js:38036:26) at reconcileChildFibers (http://localhost:3001/static/js/0.chunk.js:38435:18) at reconcileChildren (http://localhost:3001/static/js/0.chunk.js:40294:32) at updateHostComponent (http://localhost:3001/static/js/0.chunk.js:40771:7) at beginWork (http://localhost:3001/static/js/0.chunk.js:41595:18) at performUnitOfWork (http://localhost:3001/static/js/0.chunk.js:45605:16) at workLoop (http://localhost:3001/static/js/0.chunk.js:45646:28) at HTMLUnknownElement.callCallback (http://localhost:3001/static/js/0.chunk.js:25473:18) at Object.invokeGuardedCallbackDev (http://localhost:3001/static/js/0.chunk.js:25522:20) at invokeGuardedCallback (http://localhost:3001/static/js/0.chunk.js:25576:35) at replayUnitOfWork (http://localhost:3001/static/js/0.chunk.js:44829:9) at renderRoot (http://localhost:3001/static/js/0.chunk.js:45759:17) at performWorkOnRoot (http://localhost:3001/static/js/0.chunk.js:46683:11) at performWork (http://localhost:3001/static/js/0.chunk.js:46593:11) at performSyncWork (http://localhost:3001/static/js/0.chunk.js:46567:7) at requestWork (http://localhost:3001/static/js/0.chunk.js:46422:9) at scheduleWork (http://localhost:3001/static/js/0.chunk.js:46235:9) at Object.enqueueSetState (http://localhost:3001/static/js/0.chunk.js:36921:9) at Formik.push../node_modules/react/cjs/react.development.js.Component.setState (http://localhost:3001/static/js/0.chunk.js:52349:20) at http://localhost:3001/static/js/0.chunk.js:10861:17

Expected behavior

I expect to check the validation of the email Input field

Software Version(s)
Formik 1.5.8
React 16.6.0
TypeScript
Browser chrom
npm/Yarn
Operating System
ivankoleda commented 5 years ago

I tried to reproduce the issue here https://codesandbox.io/s/formik-example-z1yuc end up getting similar error.

I suppose if there is a form level validation in the code above, there is no field level validation so there is no validate prop passed down to Field like in an example in docs. validateField function assumes that the field you passed have it's own validate prop defined (source).

So the solution might be splitting validation logic into validate props of each Field https://codesandbox.io/s/formik-example-lu6yu Hope that will help with your problem.

al-ani commented 5 years ago

I am using the MATERIAL-UI components, therefore I cannot use Field component. Are you going to fix this bug?

michielmetcake commented 5 years ago

In fact you can use your own JSX component, so is Material UI. And you will get fieldProps passes as argument in the render function.

// Renders an HTML <input> and passes FieldProps field property
<Field
  name="firstName"
  render={({ field /* { name, value, onChange, onBlur } */ }) => (
    <input {...field} type="text" placeholder="firstName" />
  )}
/>

// Renders an HTML <input> and disables it while form is submitting
<Field
  name="lastName"
  render={({ field, form: { isSubmitting } }) => (
    <input {...field} disabled={isSubmitting} type="text" placeholder="lastName" />
  )}
/>

// Renders an HTML <input> with custom error <div> element
<Field
  name="lastName"
  render={({ field, form: { touched, errors } }) => (
    <div>
      <input {...field} type="text" placeholder="lastName" />
      {touched[field.name] &&
        errors[field.name] && <div className="error">{errors[field.name]}</div>}
    </div>
  )}
/>
SachinRaghunathan commented 4 years ago

I like defining the form level validation using Yup. But then, i also want to be able to validate specific fields. Is there no way i can do that?