0

Probably a very basic question, but I can't figure it out. I have a form that uses the following variables as state which returns true/false:

ccValid, dateValid, cvvValid, nameValid, addressValid

I want to have one variable that is equal to all of these.

Ex.

const formValid = {ccValid, dateValid, cvvValid, nameValid, addressValid} = this.state

2 Answers 2

1

If you mean you want a single Boolean variable that can be read at anytime to learn the form's current validity state, defined as whether the form's properties ccValid, dateValid, cvvValid, nameValid, and addressValid are all equal to true, you can add a getter to the form:

Object.defineProperty(form, 'valid', {
  get: function() { return this.ccValid && this.dateValid && this.cvvValid && this.nameValid && this.addressValid; }
});

console.log(form.valid); // always logs the current form state
Sign up to request clarification or add additional context in comments.

Comments

0

You can store all those variables in a single object in the state. For example,

class FormComponent extends React.Component {
   constructor(props) {
       super(props);
       this.state = {
          validations: {
             ccValid: false,
             dateValid: false,
             cvvValid: false,
             nameValid: false,
             addressValid: false,
        }
      }
    }
}

When you need to access each variable, you can destructure the validations property from the state, for example

const { validations } = this.state;

// you have access to validations.ccValid, validations.dateValid etc.

Let me know if this resolved the issue, or you were looking for something completely different.

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.