Skip to content

Field with reactive "required" retains invalid state after becoming optional #344

@kettanaito

Description

@kettanaito

Environment

  • react-advanaced-form: 1.6.0

What

Current behavior

When a field with the reactive required prop changes its value to false, the subscriber field is still validated to invalid: true.

Expected behavior

A field with reactive prop (i.e. required) must be validated properly when the reactive prop value toggles.

Why

  1. Validation originating from reactive resolver is forced: true
  2. Thus, shouldValidateSync predicate resolves to true
  3. Thus, relevant rules.type.tel rule is executed on an empty field
  4. Resulting into invalid field from the validation.

The reason is that the validation resolver is invoked when it shouldn't. The relation with the forced nature of reactive validation is not correct.

How

import React from 'react'
import { Form } from 'react-advanced-form'
import { Input, Checkbox } from '@examples/fields'

const rules = {
  type: {
    tel: ({ value }) => /^\+420/.test(value),
  },
}

export default class FooExample extends React.Component {
  render() {
    return (
      <Form rules={rules}>
        <Input
          type="tel"
          name="phone"
          label="Phone"
          required={({ get }) => get(['some', 'checked'])}
        />
        <Checkbox name="some" label="Accept" />
      </Form>
    )
  }
}

Thank you notes to @ludovitkapusta for reporting this issue.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions