[英]Using Yup how do i make sure that 2 fields need to be different
here's my code, i'm trying to force the two date fields always to be different, the end date must be at least 1 day after the start date.这是我的代码,我试图强制两个日期字段始终不同,结束日期必须至少比开始日期晚 1 天。
const EditSchema = Yup.object().shape({
DT_Inicio: Yup.date()
.transform(value => (isDate(value) ? undefined : value))
.typeError(intl.formatMessage({ id: 'GLOBAL.VALIDATION.REQUIRED' }))
.required(intl.formatMessage({ id: 'GLOBAL.VALIDATION.REQUIRED' })),
DT_Final: Yup.date()
.min(Yup.ref('DT_Inicio'), intl.formatMessage({id: "DATE.VALIDATION.ENDDATE"}))
.transform(value => (isDate(value) ? undefined : value))
.typeError(intl.formatMessage({ id: 'GLOBAL.VALIDATION.REQUIRED' }))
.required(intl.formatMessage({ id: 'GLOBAL.VALIDATION.REQUIRED' }))
});
Here is the component i've created.这是我创建的组件。
<div className="col-lg-3">
<InputDate
id="DT_InicioForm"
defaultValue={edit ? values.DT_Inicio : ''}
invalid={errors.DT_Inicio}
label={intl.formatMessage({
id: 'DATE.LABEL.DT_INICIO'
})}
onChange={e => {
setFieldValue('DT_Inicio', e.target.value);
}}
/>
</div>
<div className="col-lg-3">
<InputDate
id="DT_FinalForm"
defaultValue={values.DT_Final}
invalid={errors.DT_Final}
label={intl.formatMessage({
id: 'DATE.LABEL.DT_FINAL'
})}
onChange={e => {
setFieldValue('DT_Final', e.target.value);
}}
/>
</div>
After hours trying to figure out how to do it.几个小时后试图弄清楚如何做到这一点。
here's the code这是代码
.when('YourDateHere', (st, schema) => {
st.setHours(st.getHours() + 1);
return schema.min(st)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.