Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kettanaito opened this issue Dec 3, 2018 · 2 comments
Assignees
Labels
Milestone

Comments

@kettanaito
Copy link
Owner

kettanaito commented Dec 3, 2018

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.

@kettanaito kettanaito added bug needs:testing scope:validation Changes affect validation. labels Dec 3, 2018
@kettanaito kettanaito added this to the Bugs milestone Dec 3, 2018
@kettanaito
Copy link
Owner Author

kettanaito commented Dec 3, 2018

Removing the forced nature of reactive calls to field validation fixed the issue. If a field state is reset via resetValidationState, then there is no need to force the validation, as the field is treated like it hasn't been validated previously.

This means that the predicates of shouldValidateSync (especially !fieldProps.validatedSync) will resolve properly.

@kettanaito
Copy link
Owner Author

kettanaito commented Dec 3, 2018

Published under 1.6.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant