简体   繁体   中英

How can I compare and valiudate value of two data picker with form item rules in ant design?

 <Col className='input-forms' xs={24} md={3}> <Form.Item label='start'> {getFieldDecorator('start', { rules: [ { required: true, message: 'Start Date is required' } ] })( <DatePicker format='DD-MMM-YYYY' /> )} </Form.Item> </Col> <Col className='input-forms' xs={24} md={3}> <Form.Item label='end'> {getFieldDecorator('end', { rules: [ { type: 'object', required: true, message: 'End Date is required' }] })( <DatePicker format='DD-MMM-YYYY' /> )} </Form.Item> </Col>

I have two data picker which I want to validate them base on each other : 1:start should not be bigger than the end 2-should not be empty

I have no idea what should I implement invalidation?

also I am going to implement by rule

<Form.Item
                    name="EndDatetime"
                    label="End Date & Time:"
                    rules={[
                      {
                        required: true,
                        message: "Please select product warranty type!",
                      },
                      {
                        validator: async (_, endDatetime) => {
                          var startDatetime =
                            formRef.current.getFieldValue("StartDatetime");
                          if (startDatetime!=null)
                            if (endDatetime <= startDatetime) {
                              return Promise.reject(
                                "Must be greater than Start Date & Time."
                              );
                            }
                        },
                      },
                    ]}
                  >
                    <DatePicker
                      format={"YYYY-MM-DD hh:mm:ss"}
                      showTime
                      size="large"
                      className="w-100"
                    />
                  </Form.Item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM