2
\$\begingroup\$

I was writing some validation logic for a React-native app and thought I had so many bools flying around in my code!

Is there any way to reduce redundancy in this code or is this the best that can be done?

_validatePhone() {
    if (!validate.validatePhone(this.state.phone)) {
      this.setState({ phoneError: true });
      return false;
    }
  return true;
}

_validateName() {
    if (!validate.validateName(this.state.name)) {
      this.setState({ nameError: true });
      return false;
    }
    return true;
  }

  _validateForm() {
    this._resetErrors(); // Reset errors before validating
    if (!this._validateName()) {
      return false;
    } else if (!this._validatePhone()) {
      return false;
    }
    return true;
  }
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

_validateForm can be reduced to

_validateForm() {
    this._resetErrors(); // Reset errors before validating
    return this._validateName() && this._validatePhone();
}
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.