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

Field Array validation not working as expected #3

Closed
davidhenley opened this issue Dec 7, 2017 · 19 comments
Closed

Field Array validation not working as expected #3

davidhenley opened this issue Dec 7, 2017 · 19 comments
Labels

Comments

@davidhenley
Copy link

davidhenley commented Dec 7, 2017

@erikras

As you can see the validation is returning correctly, but the form errors are not showing for the field array.

Try typing in a first name.

Also try adding 6 customers, each with just a last name. You only get 3 form errors

Edit 🏁 React Final Form - Field Arrays

@davidhenley davidhenley changed the title Example on how to validate FieldArray? Field Array validation not working as expected Dec 9, 2017
@davidhenley
Copy link
Author

davidhenley commented Dec 11, 2017

@erikras Still not working correctly in 1.0.1

Steps to reproduce
Add 6 customers, each with just a last name.

What happens
You only see 3 form errors

Expected
To see 6 form errors, one for each customer

@erikras
Copy link
Member

erikras commented Dec 11, 2017

Yep, that's a bug. Thanks!

@erikras erikras added the bug label Dec 11, 2017
@davidhenley
Copy link
Author

davidhenley commented Dec 12, 2017

Formik's FieldArray has the same issue right now (and touched issues). Array validation seems to be a tricky thing. Redux-Form is the only react form tool that handles these gracefully. This is my last issue with creating all the forms for my company's site.

@erikras
Copy link
Member

erikras commented Dec 13, 2017

Fix published in final-form@1.3.3.

@erikras
Copy link
Member

erikras commented Dec 13, 2017

Hmm.. Seems to not be fixed in your sandbox... Reopening....

@erikras erikras reopened this Dec 13, 2017
@erikras
Copy link
Member

erikras commented Dec 13, 2017

Something went wrong in the publishing of v1.3.3. Fixed in v1.3.4.

One thing, if your array is full of objects, you need to push an object.

push('customers', undefined) // ❌

push('customers', {}) // ✅

@erikras erikras closed this as completed Dec 13, 2017
@davidhenley
Copy link
Author

davidhenley commented Dec 13, 2017

My meta.error is still undefined for any Field in the field array, even though the form errors are working correctly.

FieldArray errors and meta.touched are showing and hiding correctly.

My validate function:

const productValidate = values => {
  const errorArray = [];

  values.forEach(value => {
    const errors = {};

    if (!value.cases && !value.bottles) {
      errors.cases = 'Quantity Required';
      errors.bottles = 'Quantity Required';
    }

    errorArray.push(errors);
  });

  return errorArray;
}

@erikras
Copy link
Member

erikras commented Dec 13, 2017

Show updated sandbox?

@davidhenley
Copy link
Author

OK. Working on it now.

@davidhenley
Copy link
Author

davidhenley commented Dec 13, 2017

Yep. Here it is. Showing null for error. Also, try adding 3-4 customers with last names. and look at form errors. They don't match up. Only showing one form error for the entire array.

Edit RFF Field Array Problem

@erikras
Copy link
Member

erikras commented Dec 13, 2017

I'm not seeing the null error. Is it being displayed somewhere?

I did find another bug from this sandbox, though, so thanks for that.

@davidhenley
Copy link
Author

@erikras it's in the console.

@erikras
Copy link
Member

erikras commented Dec 13, 2017

Duh. 🙄🤦‍♂️

Okay, escalating this issue back to "bug" status.

@erikras
Copy link
Member

erikras commented Dec 14, 2017

Published fix in v1.3.5.

@davidhenley
Copy link
Author

Verified. Thank you for all your help!

@erikras
Copy link
Member

erikras commented Dec 14, 2017

And thank you for kicking the tires! 👍

@davidhenley
Copy link
Author

davidhenley commented Dec 14, 2017

One thing to note, is if the array is full of empty objects, you need to return undefined in the validate to mark the form as valid.

My validate function now looks like this:

const productValidate = values => {
  const errorArray = [];

  values.forEach(value => {
    const errors = {};

    if (!value.name) errors.name = 'Required';
    if (!value.cases && !value.bottles) {
      errors.cases = 'Required';
      errors.bottles = 'Required';
    }
    if (!value.billback) errors.billback = 'Required';

    errorArray.push(errors);
  });

  if (errorArray.some(object => !_.isEmpty(object))) {
    return errorArray;
  } else {
    return undefined;
  }
}

@huygn
Copy link

huygn commented Dec 14, 2017

Yea I have the same concern as above, errorArray must keeps empty objects to ensure error message goes into the correct array field. I ended up with not using onSubmit and write isValid(errors) for my custom submit method, feels really dirty :/.

@davidhenley
Copy link
Author

davidhenley commented Dec 14, 2017

Yeah, I tried to return undefined for each item, then realized the errors weren't mapping anymore. Mine isn't that bad. I mean the only thing I think that @erikras could do is use the same some function on his end to check if all items are empty then return undefined if they are.

This would allow us to go back to returning just the array.

I would try to do a pull request but I've never done one and not sure what I'm doing.

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