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

Deep form structure and deep validation #21

Closed
vslinko opened this issue Aug 13, 2015 · 8 comments
Closed

Deep form structure and deep validation #21

vslinko opened this issue Aug 13, 2015 · 8 comments

Comments

@vslinko
Copy link
Contributor

vslinko commented Aug 13, 2015

Hi! Thank you for your library.

I want to use redux-form with complex deep structures. Simple example of complex structure I want to use:

{
  users: Array[
    {
      name: string,
      group: {
        name: string
      }
    }
  ],
  confirmed: boolean
}

What are you think, is it possible to implement in redux-form?

Also I have validator for complex deep structures.
I'm in deep refactoring right now, but I'll release a new version soon with async validation, simpler API, etc.
So, could you change API of validator from ({}) => {} to something like that:

type ValidatorResult = {valid: true, [key: string]: any}
type Validator = (data: Object) => ValidatorResult | Promise<ValidatorResult>

And check only for valid property, not for every object key.
With that API I could use my validation library with your form library.

@vslinko
Copy link
Contributor Author

vslinko commented Aug 13, 2015

Your example of validator with my API suggestion:

function contactValidation(data) {
  const errors = {valid: true};
  if(!data.name) {
    errors.name = 'Required';
    errors.valid = false;
  }
  if(data.address && data.address.length > 50) {
    errors.address = 'Must be fewer than 50 characters';
    errors.valid = false;
  }
  if(!data.phone) {
    errors.phone = 'Required';
    errors.valid = false;
  } else if(!/\d{3}-\d{3}-\d{4}/.test(data.phone)) {
    errors.phone = 'Phone must match the form "999-999-9999"';
    errors.valid = false;
  }
  return errors;
}

@erikras
Copy link
Member

erikras commented Aug 13, 2015

That seems perfectly reasonable.

@vslinko
Copy link
Contributor Author

vslinko commented Aug 17, 2015

I see you done with validator API, thank you.
But what are you think about complex deep structures?

@erikras
Copy link
Member

erikras commented Aug 17, 2015

Oh, does your library not bubble up the valid:false's up to the top?

I don't think it's redux-form's responsibility to implement fancy validation. That should be up to the user of the library.

@vslinko
Copy link
Contributor Author

vslinko commented Aug 17, 2015

I probably put it unclear.

I talking about form data, not validation. Currenty redux-form supports only flat forms:

createFormReducer('contacts', ['name', 'address', 'phone'])

Do you have plans to support nested forms in redux-form?

@erikras
Copy link
Member

erikras commented Aug 17, 2015

Ah! I see.

I'm not sure. When things get deep, you end up with complex schemas. Name-value pairs are so delightfully simple. If someone (you?) could come up with a good proposal for how to do deep forms that still allowed for a simple flat form api, I'd be willing to consider it. Nothing is springing immediately to mind, and most of my form needs are pretty flat.

@vslinko
Copy link
Contributor Author

vslinko commented Aug 17, 2015

That's great. When I'll build next complex form I'll try to create PR with my solution.

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants