question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Supply touched object to validation method

See original GitHub issue

Feature

Current Behavior

Currently the validation method takes in as parameters the values from the form and props. In my current situation, I have to show an error on input A when input B has been interacted with but only if input A has been touched. Inside the validation method, there is no way to grab the information of the touched values and so it makes this type of validation really difficult.

Desired Behavior

Have access to the touched object in the validation method.

Suggested Solutions

Pass the touched object to the validation method.

  • Formik Version: 0.11.11
  • OS: Windows 10
  • Node Version: 8.9.4
  • Package Manager and version: npm 5.6.0

Opinions?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
hvolschenkcommented, Mar 1, 2018

Hiya,

There are definitely situations where this would be useful, for example:

I have a form with two fields, monthlyAmount and yearlyAmount. A user only needs to fill in one of the two.

If the user focuses on monthlyAmount, tabs off, and focuses yearlyAmount, and tabs off (without filling in any value in either) an error needs to be displayed in both. (monthlyAmount - Monthly, or, and yearlyAmount - Yearly amount required). This means I need to wait until both are touched before validating either.

However, if the user focuses on monthlyAmount, fills in an incorrect value, and tabs off, an error needs to be displayed immediately (monthylAmount - Enter a monthly value less than 100).

Here is a sample of the code that will work if I am supplied with touched in my validation method.

form.js

import { withFormik } from 'formik'

import Component from './component';

const validate = (values, props, touched) => {
  const errors = {};
  if (!values.monthlyAmount && touched.monthlyAmount && touched.yearlyAmount) {
    errors.monthlyAmount = 'Monthly, or';
  }
  if (!values.yearlyAmount && touched.monthlyAmount && touched.yearlyAmount) {
    errors.yearlyAmount = 'yearly amount is required';
  }
  if (values.monthlyAmount && values.monthlyAmount > 100 && touched.monthlyAmount) {
    errors.monthlyAmount = 'Enter a monthly value less than 100';
  }
  if (values.yearlyAmount && values.yearlyAmount > 100 && touched.yearlyAmount) {
    errors.yearlyAmount = 'Enter a yearly value less than 100';
  }
};

export default withFormik({ validate })(Component);

component.jsx

import React from 'react';

const Component = ({ errors, handleBlur, handleChange, handleSubmit, touched, values }) => (
  <form onSubmit={handleSubmit}>
    <input
      name="monthlyAmount"
      onBlur={handleBlur}
      onChange={handleChange}
      type="number"
      value={values.monthlyAmount}
    />
    <p>{touched.monthlyAmount && errors.monthlyAmount}</p>
    <input
      name="yearlyAmount"
      onBlur={handleBlur}
      onChange={handleChange}
      type="number"
      value={values.yearlyAmount}
    />
    <p>{touched.yearlyAmount && errors.yearlyAmount}</p>
  </form>
);

export default Component;

Maybe you can suggest a clean solution without using touched?

2reactions
jaredpalmercommented, Feb 22, 2018

Right so why can’t you just do something like {touched.fieldA && touched.fieldB && errors.fieldB} ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Errors - Formik for Beginners
You supply it with a name . When that fields contains an error and the field has been touched. Then it will display...
Read more >
React form validation solutions: An ultimate roundup
This roundup is a comprehensive look at some of the most popular solutions for form management and validation in React.
Read more >
useForm - trigger - Simple React forms validation
Manually triggers form or input validation. This method is also useful when you have dependant validation (input validation depends on another input's ...
Read more >
how to write validation in sencha touch / ext js model
Errors object return by the validate method of the model. So we can do something like: var record = Ext.create('Ext.data.
Read more >
withFormik() | Formik
Your form submission handler. It is passed your forms values and the "FormikBag", which includes an object containing a subset of the injected...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found