I'm quite unsure how to do a form validation within react.
I want to display an error message on the current page saying. However, the password validation is ignored so their is no error that shows on the page.
Password must be at least characters
Maybe i'm not using conditional rendering right
SignUp.js (snippet for demonstration purpose)
constructor(props){
super(props);
this.state ={
errors: {},
}
handleSubmit(event) {
event.preventDefault();
const email = this.email.value;
const password = this.password.value;
if(password.length > 6){
this.state.errors.password= "Password must be at least 6 characters";
}
const creds = {email, password}
if(creds){
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
ref={(input) => this.email = input}
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.password > 6 &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
ref={(input) => this.password = input}
value={this.state.password}
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}