0

I'm having an input validation problem thats allowing the form to submit without having any selectorValues added. The check I have seems to only check for input inside the textarea but doesn't account for the Add button being pressed.

Here's a sandbox reproducing the issue.

I'm using Semantic-ui-react so my <Form.Field /> looks like this:

<Form.Field required>
  <label>Selector Values:</label>
  <TextArea
    type="text"
    placeholder="Enter selector values 1-by-1 or as a comma seperated list."
    value={this.state.selectorValue}
    onChange={this.handleSelectorValueChange}
    required={!this.state.selectorValues.length}
  />
  <Button positive fluid onClick={this.addSelectorValue}>
    Add
  </Button>
  <ul>
    {this.state.selectorValues.map((value, index) => {
      return (
        <Card>
          <Card.Content>
            {value}
            <Button
              size="mini"
              compact
              floated="right"
              basic
              color="red"
              onClick={this.removeSelectorValue.bind(this, index)}
            >
              X
            </Button>
          </Card.Content>
        </Card>
      );
    })}
  </ul>
</Form.Field>

So in the above, <TextArea> has a required prop: !this.state.selectorValues.length. This is only checking for input inside the textarea, it should check that the value has been added by pressing the Add button before allowing the form to submit.

4
  • Do you want to prevent adding empty values to selectorValues ? or there is something else ? Commented Apr 24, 2019 at 21:48
  • Yes I also want to prevent that Commented Apr 24, 2019 at 21:50
  • My answer will prevent adding empty values but i'm not sure i get what else is the problem can you explain what else needs to be fixed ? Commented Apr 24, 2019 at 21:51
  • If you start typing a value into the text box and hit submit it allows the form to be submitted without the value actually having to be added via the button. Commented Apr 24, 2019 at 21:54

2 Answers 2

1

In your addSelectorValue add a check to see if this.state.selectorValue it not empty, if it is just return, this will prevent adding empty values to selectorValues

addSelectorValue = e => {
    e.stopPropagation();
    e.preventDefault();

    if (!this.state.selectorValue) return;
    //continue if this.state.selectorValue has a value
};

Before submitting add a check to see if this.selectorValues is empty, if so focus on textarea.

To focus we need to first create a ref to our textarea.

Create a ref to be attached to a dom element

textareaRef = React.createRef();

// will use our ref to focus the element
focusTextarea = () => {
  this.textareaRef.current.focus();
}

handleSubmit = () => {
  const { selectorValues } = this.state;
  if (!selectorValues.length) {
    // call our focusTextarea function when selectorValues is empty
    this.focusTextarea();
    return;
  }
  this.setState({ submittedSelectorValues: selectorValues });
};

// attach our ref to Textarea
<Textarea ref={this.textareaRef} />
Sign up to request clarification or add additional context in comments.

2 Comments

This does work. Is there a way to make it an alert focused on the input in question instead of a browser alert? I will probably need a schema validation like yup to achieve this.
I've edited my answer to focus our Textarea when handling validation
1

After some search ... required prop is for decorational purposes only - adding astrisk to field label.

It has nothing to form validation. You need a separate solution for that - try formik or set some condition within submit handler.

Formik plays nicely with yup validation schema - suitable for more complex, dynamic requirements.

1 Comment

<Form> from semantic ui react handles data just like vanilla react <form />. So the use of the required prop inside <TextArea> has a purpose. <Form.Field /> required prop is indeed decorational in the sense you described.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.