1

I am creating react form and validating form fields, If the field is empty error message shown below and state is set in errors.

I need to check the select field also, the select field is an array of objects. How to check the select option was empty or not?

check the example code https://codesandbox.io/s/flamboyant-flower-2u1xw

3
  • Just a recommendation, you should take a look at formik Commented Sep 10, 2019 at 11:31
  • you want to check if at least one option as been selection within the select field? Commented Sep 10, 2019 at 11:54
  • You need to initially assume the form is valid, then set the variable to false if an error is found: codesandbox.io/s/wild-resonance-msq12 Commented Sep 10, 2019 at 12:04

2 Answers 2

1

You already save the selected options into your state. So within your validate function, you just have to access the state and check the required properties. Also make sure you stay consistent with your variable names. Within your validate function, you name your return value formIsValid but later when you call the function, you use the return value as errors which is confusing. I renamed formIsValid to hasErrors to stay consistent.

  validate() {
    let required = ["name", "description"];
    let errors = {};
    let hasErrors = false;
    let data = this.state.data;

    required.forEach(w => {
      if (!data[w]) {
        hasErrors = true;
        errors[w] = "Please fill the required fields";
      }
    });
    this.setState({ errors: errors });

    // check if at least one option has been selected
    let currentLength = data.roles.length;
    if (!length) {
       hasErrors = true;
       erros["roles"] = "Please fill the required fields";
    }

    return formIsValid;
  }

And just add a error span as you did before:

        <div className="form-group">
          <label>Roles</label>
          <Select
            isMulti
            value={this.state.roles}
            options={options}
            onChange={e => this.handleAccessChange(e)}
          />
          <span>{this.state.errors["roles"]}</span>
        </div>
Sign up to request clarification or add additional context in comments.

Comments

1

In your code there is no logic for validating roles.

So something like the following is needed:

  validate() {
    let required = ["name", "description"];
    let errors = {};
    let formIsValid = false;
    let data = this.state.data;

    required.forEach(w => {
      if (!data[w]) {
        formIsValid = true;
        errors[w] = "Please fill the required fields";
      }
    });

    if (this.state.roles.length === 0) {
        formIsValid = false;
        errors["roles"] = "Please select at least one role";
    }

    this.setState({ errors: errors });
    return formIsValid;
  }

https://codesandbox.io/s/funny-yalow-udgq7

Comments

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.