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.

[request] FormSpy should receive FormApi

See original GitHub issue

Are you submitting a bug report or a feature request?

feature request

What is the current behavior?

FormSpy provides access to internal FormState, but does not expose a way to comprehend the registered fields.

What is the expected behavior?

FormSpy is passed FormApi in addition to FormState so that one may call getRegisteredFields: () => string[]

What’s your environment?

RFF v2.1.1 React v16.2.0

Workaround

It looks like I can get FormApi from the context as reactFinalForm, but I think it a useful addition to provide the FormApi to the FormSpy unless there is a reason to protect it.

BTW - so far everything has been fantastic. Very solid library, almost no issues converting a mature codebase from react-formal over to this.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
rosskevincommented, Jan 10, 2018

That is so far the only reason I need to access the context. I have component that is a catch all for errors that do not match current fields (e.g. field level validation vs record level validation) and displays them above the submit.

Here’s the use:

export interface RenderArgs {
  errors: React.ReactNode
}

export interface UnmappedErrorsProps {
  children: (renderArgs: RenderArgs) => React.ReactNode
}

class UnmappedErrors extends React.Component<UnmappedErrorsProps> {
  public static contextTypes = {
    reactFinalForm: PropTypes.object.isRequired,
  }

  public render() {
    const { children } = this.props
    return (
      <FormSpy>
        {({ errors }) => {
          const fieldNames = (this.context.reactFinalForm as FormApi).getRegisteredFields()
          log.debug('render', fieldNames, errors)

          const unmappedErrors: string[] = []
          Object.getOwnPropertyNames(errors).forEach(fieldName => {
            if (!fieldNames.includes(fieldName)) {
              const fullMessage = `${fieldName} ${errors[fieldName]}`
              unmappedErrors.push(fullMessage)
              log.debug(fieldName, ': ', fullMessage)
            }
          })
          return children({ errors: unmappedErrors })
        }}
      </FormSpy>
    )
  }
}
0reactions
lock[bot]commented, Jan 11, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Final Form Docs – `<FormSpy/>`
A component that subscribes to form state, and injects both form state and the form instance via a render prop. The <FormSpy/> will...
Read more >
FormSpy: provide form API to onChange callback #412 - GitHub
Are you submitting a bug report or a feature request? Feature request What is the current behavior? The FormSpy#onChange callback is only ...
Read more >
Forms API Overview - HubSpot API
Using the Forms API​​ You can select from two different endpoints: Submit data to a form (Supporting Authentication) Submit data to a form....
Read more >
Simple way to update form values depending on changes with ...
Here is a version with a combination of FormSpy component, useRef hook and the form.reset api. https://codesandbox.io/embed/react-final-form- ...
Read more >
The Forms API - Django documentation
This document covers the gritty details of Django's forms API. ... The validation routines will only get called once, regardless of how many...
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