A cleaner way is to use the joi-browser package. In a state, you should have an error object that includes all errors in the form. Initially, it should be installed on an empty object. Create a schema;
import Joi from "joi-browser"; schema = { username: Joi.string() .required() .label("Username") .email(), password: Joi.string() .required() .label("Password") .min(8) .regex(/^(?=.*\d)(?=.*[az])(?=.*[AZ])(?=.*[^a-zA-Z0-9]).{8,1024}$/)
Then check the form using the diagram:
validate = () => { const options = { abortEarly: false }; const result = Joi.validate(this.state.data, this.schema, options); console.log(data) // always analyze your data if (!result.error) return null; const errors = {}; for (let item of result.error.details) errors[item.path[0]] = item.message; //in details array, there are 2 properties,path and message.path is the name of the input, message is the error message for that input. return errors; };
Before submitting the form, check the form:
handleSubmit = e => { e.preventDefault(); const errors = this.validate(); //will return an object console.log(errors); this.setState({ errors: errors || {} }); //in line 9 if we return {}, we dont need {} here if (errors) return; //so we dont need to call the server alert("success"); //if there is no error call the server this.dosubmit(); };
source share