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.

Cannot easily pass errors from outside the form into it

See original GitHub issue

I have a form that will have some validation errors coming from the parent component. I need to take those errors and pass them to the form state. However, informed doesn’t seem to provide such functionality. I could do it via setError function, but I would like to be able to achieve that without having to convert my components to stateful components. Is there such a way?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JaffParkercommented, Oct 11, 2018

I’m a big fan of stateless components, so here’s what I try to do:

import React from 'react'
import { Form } from 'informed'

export const MyForm = ({outsideErrors}) =>
  <Form>
    {({
      formApi: { fieldExists, setError }
    }) => {
      mapValues(outsideErrors, (msg, field) => {
        fieldExists(field) && setError(field, msg)
      })

      return 'I am a form'
    }}
  </Form>

When I tried this approach, I realized that it causes the state to be updated inside the render function which causes it to re-render and update state again, ie endless loop.

I could technically convert it to a stateful component and use componentWillReceiveProps, but I’d rather avoid that. I would propose instead a prop, something like errors on both Form and Field component that would take an error object and merge it with validation errors. Something like this:

import React from 'react'
import { Form } from 'informed'

export const MyForm = ({outsideErrors}) =>
  <Form errors={outsideErrors}>
    {({
      formState: { errors }
    }) => {
      console.log(errors) // { ...outsideErrors, ...validationErrors }

      return 'I am a form'
    }}
  </Form>

Not sure if it’s functionality that others would be interested in, but it probably wouldn’t hurt. I can put together a PR for that.

0reactions
joepuzzocommented, Feb 7, 2019

Closing as V2 is out and this should work as expected

Read more comments on GitHub >

github_iconTop Results From Across the Web

passing form errors from outside · Issue #33 · jaredpalmer/formik
Hi :) What's the best way to pass the errors from outside? What I'm trying to do is to get network errors from...
Read more >
How to display <form:errors> outside <form:form> in Spring?
Show activity on this post. I'm new to Spring and I've been having some trouble trying to show a form errors. The form,...
Read more >
Form Validation Errors - how to track them and what to do ...
1) a user entered an invalid data into a form field, for example, an email without "@" character; 2) a user entered a...
Read more >
Custom field validation with errors outside of JSONForms and ...
Hi, I'd like to run some custom validation logic for a field in my JSONForms form. If this logic finds a problem, it...
Read more >
How to Report Errors in Forms: 10 Design Guidelines
Help users recover from errors by clearly identifying the problems and allowing users to access and correct erroneous fields easily.
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