Skip to content

Bug: Constraint Validation API not reflected on Textarea's value attribute change #19474

Closed as not planned
@tdiluzio

Description

@tdiluzio

Hi,

thank you very much for what you do and you've been doing so far.

I've been working on some form validations via the Validation Constraints API and I've noticed that event.target.validity read-only object doesn't get updated and is always valid although some constraints are set when the value attribute is set to the Textarea.

On the other hand, when value is passed as children validity gets updated as expected but I'm being warned to Warning: Use the defaultValue or value props instead of setting children on <textarea>

React version: 16.13.1

Steps To Reproduce

Here's how you can reproduce:

  1. setup this basic app component with one Textarea element, set some constraints and add the value attribute as you normally would on any form.

  2. add another Textarea element but instead of passing value as an attribute pass it as children see code below

import React, { useState } from 'react';

function App() {
  const [state, setState] = useState({
    first: '',
    second: '',
  });

  function handleTextChange({ target }) {
    const { name, value, validity } = target;

    setState({
      [name]: value,
    });

    console.log(`${name} validity:`, validity);
  }
  return (
    <div className="App">
      <textarea
        name="first"
        placeholder="First"
        value={state.first}
        onChange={handleTextChange}
        required={true}
        minLength={10}
      />

      <textarea
        name="second"
        placeholder="Second"
        onChange={handleTextChange}
        required={true}
        minLength={10}
      >
        {state.second}
      </textarea>
    </div>
  );
}

export default App;
  1. Open your console

  2. Type at least one character into the first Textarea see screenshot
    Capture d’écran 2020-07-28 à 15 43 33

  3. Type at least one character into the second Textarea see screenshot
    Capture d’écran 2020-07-28 à 15 44 02

The current behavior

You'll notice that the first Textarea ignores the validation constraints set

The expected behavior

Validation constraints should be set and field should be invalid

Link to code example

Please find sample code at https://github.com/tdiluzio/react-dom-textarea-bug

Demo

Here's the URL to the demo https://tdiluzio.github.io/react-dom-textarea-bug/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleAutomatically closed due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions