是的,当用户单击提交按钮时,不会验证复选框
Yup is not validating a checkbox when the user clicks the submit button
我的 React 应用程序中有以下 Yup 配置:
const schema = yup.object().shape({
email: yup.string()
.email('E-mail is not valid!')
.required('E-mail is required!'),
password: yup.string()
.min(6, 'Password has to be longer than 6 characters!')
.required('Password is required!'),
tandc: yup.boolean()
.oneOf([true], "You must accept the terms and conditions")
})
我的表单如下所示(使用 Formik):
<Form>
<div className="form-group">
<label >Email
<Field type="email" name="email" className="form-control" />
</label>
<ErrorMessage name="email" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<label >Password
<Field type="password" name="password" className="form-control" />
</label>
<ErrorMessage name="password" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<Field type="checkbox" name="tandc" className="form-check-input" id="tandc" />
<ErrorMessage name="tandc" component="div" className="invalid-feedback" />
<label htmlFor="tandc" >I agree to the Terms and Conditions
</label>
</div>
<PrimaryButton label="Login" disabled={isSubmitting} type="submit">
Submit
</PrimaryButton>
</Form>
如果用户在表单上单击提交,Yup 会验证电子邮件和密码字段但忽略复选框字段:
只有先选中然后取消选中条款和条件,我才能使复选框验证正常工作。
如何让它工作,以便在用户仅单击提交按钮时显示错误消息?
根据 Yup 存储库中的 issue,
if you're using it with formik make sure you set initialValues = {termsOfService: false}
为了
yup.boolean().oneOf([true],'Message');
要工作,您必须将该字段的初始值设置为 true
或 false
。
在你的情况下它看起来像这样
tandc : false
无论您在哪里设置初始值
使用
yup.default(false)
或
yup.default(true)
指定默认值
如果您正在使用 react-hook-form:
const schema = yup.object().shape({
...
tandc: yup.bool() // use bool instead of boolean
.oneOf([true], "You must accept the terms and
conditions")
})
我的 React 应用程序中有以下 Yup 配置:
const schema = yup.object().shape({
email: yup.string()
.email('E-mail is not valid!')
.required('E-mail is required!'),
password: yup.string()
.min(6, 'Password has to be longer than 6 characters!')
.required('Password is required!'),
tandc: yup.boolean()
.oneOf([true], "You must accept the terms and conditions")
})
我的表单如下所示(使用 Formik):
<Form>
<div className="form-group">
<label >Email
<Field type="email" name="email" className="form-control" />
</label>
<ErrorMessage name="email" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<label >Password
<Field type="password" name="password" className="form-control" />
</label>
<ErrorMessage name="password" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<Field type="checkbox" name="tandc" className="form-check-input" id="tandc" />
<ErrorMessage name="tandc" component="div" className="invalid-feedback" />
<label htmlFor="tandc" >I agree to the Terms and Conditions
</label>
</div>
<PrimaryButton label="Login" disabled={isSubmitting} type="submit">
Submit
</PrimaryButton>
</Form>
如果用户在表单上单击提交,Yup 会验证电子邮件和密码字段但忽略复选框字段:
只有先选中然后取消选中条款和条件,我才能使复选框验证正常工作。
如何让它工作,以便在用户仅单击提交按钮时显示错误消息?
根据 Yup 存储库中的 issue,
if you're using it with formik make sure you set
initialValues = {termsOfService: false}
为了
yup.boolean().oneOf([true],'Message');
要工作,您必须将该字段的初始值设置为 true
或 false
。
在你的情况下它看起来像这样
tandc : false
无论您在哪里设置初始值
使用
yup.default(false)
或
yup.default(true)
指定默认值
如果您正在使用 react-hook-form:
const schema = yup.object().shape({
...
tandc: yup.bool() // use bool instead of boolean
.oneOf([true], "You must accept the terms and
conditions")
})