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.

Field level validation dependent on other fields.

See original GitHub issue

🚀 Feature request

Current Behavior

<Field validate={(fieldValue) => ... } /> For now field level validaiton funciton gives us only field value.

Desired Behavior

<Field validate={(fieldValue, formikBag) => ... } /> I want to have access to formikBag inside of field level validation.

Suggested Solution

Pass additional argument with formikBag to validate callback function.

Who does this impact? Who is this for?

I want to have possibility to access other formik values to create dependent validation. E.g. I have Field1 and Field2. Field2 should be invalid if Field1 is true and Field2 have some values inside. Form is reused and you can compound it from smallest reuseable pieces. It means sometimes you can have Field2 defined or undefined, that is why I don’t want to use for this case global <Formik validation method.

Describe alternatives you’ve considered

  1. Global Formik validation method - no, bcs of -> I want to see validation on component Blur, and want to compound form from smallest Fields element that can be included or not dynamically.

  2. Field Field nesting just to have form values inside of second Field validation

<FIeld>
{
({form}) => <FIeld validate={value => form.values.FIELD1} > ...</Field>
}
</Field>

No. Just have a look at this. Unnecessary component just to retrieve values I should have access to.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:29
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
zveljkoviccommented, Jul 23, 2020

Any news on this?

6reactions
tonypinecommented, Apr 8, 2020

I successfully managed to get my validation to work with a little hack:

  const { validateField, values } = useFormikContext();
  const fieldValue1 = values?.fieldValue1;
  const fieldValue2 = values?.fieldValue2;

  const validate = useCallback(
    value => {
      // validation logic
    },
    [fieldValue1, fieldValue2]
  );
  ...
  const [field, meta, helpers] = useField({
    name: fieldName,
    validate
  });
  useEffect(() => {
    const timeoutId = setTimeout(() => validateField(fieldName), 50);
    return () => clearTimeout(timeoutId);
  }, [fieldName, validate, validateField]);

By the time the validate method of invoked by formik, the other field’s values weren’t propagated yet to the component updating the validate method, but with this useEffect I re-invoke the validation of the field once, and it did the trick.

Is it a hack? Yes. Does it work? Yes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validate when fields depend on one another - Stack Overflow
1 Answer 1 · Create class-level @interface . All fields are accessible. · Create ConstraintValidator class with validation logic. · Set this ...
Read more >
Validating fields with rules that depend on other fields - Vaadin
I want to validate fields that have rules that depend on value of another field. Simple example would be: public class Bean {...
Read more >
Defining a Field-Level Validation Rule - Oracle Help Center
A field-level validation rule is a constraint you can define on any standard or custom field. It is evaluated whenever the corresponding field's...
Read more >
Cross field validation in Spring and JEE - | Dev in Web
See a class level cross field validator in action. ... Yet, it is not unusual that several fields are connected to each other...
Read more >
Angular Cross Field Validation - Medium
The solution is simple. We should just add a validator to the rangeStart control, which checks the value of the rangeEnd control. Next...
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