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.

FormSection validation, how to handle?

See original GitHub issue

Is there a way to add validation (Sync) at FormSection level? If we consider the example below, i wish i could use the validation system on the address component level. It might not be a good idea, what would you suggest then to deal with nested object models and validation

const renderField = ({ input, label, type, meta: { touched, error, warning } }) => (
  <div>
    <label>{label}</label>
    <div>
      <input {...input} placeholder={label} type={type}/>
      {touched && ((error && <span>{error}</span>) || (warning && <span>{warning}</span>))}
    </div>
  </div>
)

class Address extends FormSection {
  // I need to validate this section ! Is there a Redux-From build-in solution ? 
  render(){
    return (
      <div>
        <Field name="street" type="text" component={renderField} label="Street"/>
        <Field name="city" type="text" component={renderField} label="City"/>
        <Field name="zipCode" type="text" component={renderField} label="Zip Code"/>
        <Field name="country" type="text" component={renderField} label="Country"/>
      </div>
    );
  }
}

const validate = values => {
 // do the validation for the CustomerForm (only the name field)
}

const CustomerForm = (props) => {
  const { handleSubmit, pristine, reset, submitting } = props
  return (
        <form onSubmit={handleSubmit}>
             <Field name="name" type="text" component={renderField} label="Name"/>
             <Address name={'delivery'} />
        </form>
    }
}

 export default reduxForm({
  form: 'customer', // Form name
  fields: [ 'name', 'address' ], // Form fields
  validate // <--- Validation function
})(CustomerForm);

Thank you for your help and advices

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
lnhrdtcommented, Jan 18, 2017

There isn’t FormSection level validation

Is this still true? While you’re correct that it can be added at the form level, I’ve modularized my components into Fields and FormSections for reusability and I wish I could encapsulate validation in my FormSection components just like in my Field components.

4reactions
gustavohenkecommented, Jan 18, 2017

Hi @lnhrdt, yes, this still holds true. A suggestion I can make you for now is to export the validation functions for each section/field and compose them together.

Here you go, for some good ideas: https://github.com/jfairbank/revalidate

Read more comments on GitHub >

github_iconTop Results From Across the Web

FormSection validation, how to handle? · Issue #2150 - GitHub
There isn't FormSection level validation, but it's pretty straightforward to create a validation function for your Address component, ...
Read more >
Using Validation on a FormSection with Redux Forms
As a workaround, you can use form-level validation: ... your validation function, field names will be prefixed with the FormSection's name.
Read more >
Synchronous Validation Example - Redux Form
There are two ways to provide synchronous client-side validation to your form. The first is to provide redux-form with a validation function that...
Read more >
Examples - Final Form Docs
Demonstrates how to use React Final Form to create a multi-page "wizard" form, with validation on each page. Parse and Format (and Normalize)....
Read more >
ASP.NET Core Blazor forms and input components
You can create custom validator components to process validation ... from the Example form section is used that only accepts the starship's ...
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