The check problem of antd using DatePicker in Form

first give demo, demo
has a start time and an end time, which requires that the start time must be less than the end time

reproduction step:

  1. choose a start time, such as 2018-08-23
  2. choose an end time, which is less than the start time, for example, 2018-08-22 . The end time prompts that the end time must be greater than the start time . So far, there is no problem
  3. .
  4. Select another start time, which is less than the end time, for example, 2018-08-21 . The error prompt for the end time still exists. This is the main step. how to check the end time again

clipboard.png

how to check again if you don"t use RangePicker , don"t use disabledDate , don"t customize validateStatus of Form, help, etc. I called validateFieldsAndScroll` in onChange of DatePicker , which seems to be of no use

.
Apr.28,2021

I added breakpoints to your code to debug and found the cause of the problem:

  1. validateFieldsAndScroll you can pass {force: true} as the second parameter see the document )), otherwise, the unmodified fields will not trigger the check again.
  2. the value you get in onChange is the new value, but this value has not been updated to the state of the component, that is, the value you get by calling form.getFieldValue ('timeStart') is still the old value;

for the above reasons, the solution is also relatively clear:

  1. add {force: true} parameters to validateFieldsAndScroll ;
  2. put the call to form.validateFieldsAndScroll in setTimeout (() = > {}, 0) inside

has no one ever encountered such a need?


it is fine to add custom checks to both date boxes. One example and the other one is fine by its own judgment logic

{
    getFieldDecorator('date1', { initialValue: moment(),
        rules: [{
            required: true,
            validator: (rule, value, callback) => {
            const edate = getFieldValue('date2');
            if (moment(edate).isBefore(moment(value))) {
                callback();
            } else {
                callback('you error message');
            }
        },
        message:'xxx'
    }]
})
    (
        <DatePicker
            placeholder='1'
            format='YYYY-MM-DD dddd'
            onChange={date => { /*do something*/ }}
        />
)}
Menu