teslamotors / informed

A lightweight framework and utility for building powerful forms in React applications
https://teslamotors.github.io/informed
MIT License
956 stars 173 forks source link

onSubmit - setError is not a function #432

Closed cherring07 closed 1 year ago

cherring07 commented 2 years ago

Perhaps undocumented (sorry if I have missed it)

When using the useFormAPI hook with setError within onSubmitI get the following error.

formApi.setError is not a function

const Test= () => {
  const formApi = useFormApi();

  const onSubmit = () => {
    formApi.setError('example', 'error!');
  };

  render (  
     <Form onSubmit={onSubmit}>
      <Input name="example" label="Example" />
      <button type="submit">Submit</button>)
     </Form>
     );
}
joepuzzo commented 1 year ago

You are outside the context of the form.

  1. I would highly reccomend NOT setting errors directly as there is a lifecylce for this
  2. If you do need the formApi outside of the context of a <Form you need to use <Form formApiRef={someRef}

Then you can do formApiRef.current.setValue('example', 'Foo') as an example