【发布时间】:2020-11-05 08:43:32
【问题描述】:
我想将 .test() 方法添加到 validationSchema。测试是寻找常见的电子邮件域拼写错误。例如,检查 gmail 是否拼错了“gmial”或“gnail”。
const validationSchema = Yup.object({
email: Yup.string().lowercase()
.email('You have entered an invalid email')
.required('This field is required')
.test('test-name', 'Did you mean @gmail??????', function(value){
if (value.includes('gmial' || 'gnail'))
return this.createError('')
})
<Formik>
<Form>
<div className='form-control'>
<label htmlFor='email'>Email</label>
<Field type='text' id='email' name='email' />
<ErrorMessage name='email' component={TextError} />
</div>
</Form>
</Formik>
【问题讨论】: