简体   繁体   English

使用是的,我如何确保 2 个字段需要不同

[英]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.

相关问题 如何检查以确保表单字段不是空字符串? - How do I check to make sure form fields are not empty strings? 如何使用JavaScript + jQuery确保URL是图像? - How do I make sure a URL is an image using JavaScript + jQuery? 在使用random()时,如何确保没有重复的数字? - How do I make sure there are no repeated numbers when using random()? 使用Javascript,如何确保日期范围有效? - Using Javascript, how do I make sure a date range is valid? 如何使用 Formik 和 Yup 进行服务器端验证? - How to do I do server side validation using Formik and Yup? 如何确保 null 和空字符串字段未添加到我在 Firestore Reactjs 中的文档中 - How do I make sure null and empty string fields arent added to my documents in firestore Reactjs 如何使用 jest 确保我只使用两个 arguments? - How do I use jest to make sure I am only using two arguments? 使用Recurly JS如何设置需要的不同字段 - Using Recurly JS how do I set different fields to be required 如何使用WebSockets确保消息到达目的地? - How do I make sure a message gets to its destination using WebSockets? 如何使用 React Native 和 AWS 确保每次都导入我的个人资料信息? - How do I make sure my Profile information is imported every time, using React Native and AWS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM