反应表单验证:使用formik出现错误时禁用提交按钮

时间:2020-06-18 22:21:31

标签: reactjs formik yup

我想在输入字段为空或有任何错误时首先禁用我的提交按钮,如果不是全部使用formik和yup启用它,我尝试使用dirty和isValid但它无法正常工作,即使在那里没有错误 这是我的组件:

const validationSchema = Yup.object().shape({
 phone: Yup.string()
.required("Required Field")
.min(10, "Must be a 10 Characters Long")
.max(10, "Must be a 10 Characters Long"),

passwordClient: Yup.string()
.min(8, "Minimum 8 Characters")
.required("Required Field"),
});

class Signin extends Component {
 constructor(props) {
 super(props);
 this.state = {
  phone: "",
  passwordClient: "",
  clients: [],
};
}

handleSubmit = (values) => {
Services.createClient({
  phone: this.state.phone,
  passwordClient: this.state.passwordClient,
})
  .then(console.log(this.state))
  .catch(console.log(this.state));
 };

 handleChange = (event) => {
 this.setState({ [event.target.name]: event.target.value });
 };

 render() {
 let { phone, passwordClient } = this.state;
  return (
  <div className="wrapper justify-content-center">
    <div className="form-wrapper">
      <h1>
        <b>
          <i>Create an Account</i>
        </b>
      </h1>
      <Formik
        initialValues={{
          phone,
          passwordClient,
        }}
        validationSchema={validationSchema}
        onSubmit={this.handleSubmit}
        validate={this.validate}
        enableReinitialize={true}
        validateOnBlur={true}
        validateOnChange={true}
      >
        {({ values, errors, touched, handleBlur, isValid, dirty }) => (
          <form>
            <div className="col-md-12 ">
              <input
                type="text"
                placeholder="Enter Phone Number"
                name="phone"
                onChange={this.handleChange}
                onBlur={handleBlur}
                value={this.state.phone}
                className={touched.phone && errors.phone ? "error" : null}
              />
              <Errors touched={touched.phone} message={errors.phone} />
            </div>

            <div className="col-md-12 ">
              <input
                type="password"
                placeholder="Enter password"
                name="passwordClient"
                onChange={this.handleChange}
                onBlur={handleBlur}
                value={this.state.passwordClient}
                className={
                  touched.passwordClient && errors.passwordClient
                    ? "error"
                    : null
                }
              />
              <Errors
                touched={touched.passwordClient}
                message={errors.passwordClient}
              />
            </div>
            <div className="createAccount">
              <button
                type="button"
                onClick={this.handleSubmit}
                disabled={!(isValid && dirty)}
              >
                Signin
              </button>
              <small>
                Does not have an account ? <Link to="/signup">Sign up</Link>
              </small>
            </div>
          </form>
        )}
      </Formik>
    </div>
  </div>
  );
  }
  }
 export default Signin;

您有任何解决方案的线索吗?

0 个答案:

没有答案