form icon indicating copy to clipboard operation
form copied to clipboard

Warning is sticky unless cleared by success or error

Open sfilipov opened this issue 8 years ago • 4 comments

Once the validation function returns a {warning: "message"}, it is "sticky" until there is success or error to "clear" it. What I mean by sticky, is that the field is considered to have a warning even after the validation function no longer returns a warning:

screen recording 2018-04-18 at 11 38 am

Sample validation function and form:

  render() {
    const validate = value => {
      if (value === "print warning") {
        return {warning:"some warning"}
      }
      if (value === "print error") {
        return {error:"some error"}
      }
    }

    return (
        <Form>
          {formApi => (
            <form onSubmit={formApi.submitForm}>
              <Text field="hello" id="hello" validate={validate} />
              <button type="submit" className="btn btn-primary">
                Submit
              </button>
            </form>
          )}
        </Form>
    )
  }

I tried to track down the bug in util and redux folders but did not succeed. Tested with version 3.5.2 and 3.5.3. Am I doing something wrong?

sfilipov avatar Apr 18 '18 10:04 sfilipov

Yup all you need to do is have a default return at bottom that returns an empty object! The reason u r seeing this is that you dont return unless either of those ifs are hit

joepuzzo avatar Apr 18 '18 11:04 joepuzzo

Thanks for your quick response!

My current workaround is to always return a non-null object with success inside. I was not aware of the empty object default possibility. I just gave it a try and it didn't work - behaviour is that same as above. Here's my updated validation function:

    const validate = value => {
      if (value === "print warning") {
        return {warning:"some warning"}
      }
      if (value === "print error") {
        return {error:"some error"}
      }
      return {}
    }

sfilipov avatar Apr 18 '18 11:04 sfilipov

To add to the above, the following values don't work as defaults that reset warning. Note that error and success are not affected: {} {success: null} {warning: null}

The cleanest way I've found to workaround is to return an object with one of the success, warning or error keys set to a value of {}. For instance, both of the following work: {success: {}} {warning: {}}

sfilipov avatar Apr 18 '18 11:04 sfilipov

I was wrong about the problem applying only to warning - turns out the bug affect success and warning. Error is the only one that's special and is not affected / working in all cases.

I tracked it down to the logic for validating: https://github.com/react-tools/react-form/blob/77e9ab566bca7244c66f63bd1bae0365b80743da/src/redux/actions.js#L87

What it basically does is:

  • if the result of validation is not an object and not an array - it's probably a string, so set the error to this string. Importantly, warning and success are not reset
  • if the result of validation is an object that has at least one of success, warning and error, then reset all three, and dispatch actions for the relevant ones.

The problem is - all three should not be reset only when the result is an object with a truthy key. They should always be reset (potentially at the top of the recurse function, before any checks?)

sfilipov avatar Apr 18 '18 14:04 sfilipov