form4表单验证书写方式

 

1.直接在表单项书写:

 rules={[
        {
            required: true,
            message: '请选择指标类型',
        }, {
            validator: (_, value) => {
                const num = form.getFieldValue('itemList')[field.name]['filter'].filter(c => c?.name === value)?.length || 0
                if (num > 1 && value != '') {
                    return Promise.reject(new Error('指标类型重复'));
                } else {
                    return Promise.resolve();
                }
            }
        }
        ]}

 2.单独定义并在表单项引用

 const dataCenterChecked = ({ getFieldValue }) => ({
    validator(_, value) {
      const num = getFieldValue('alarmUserList').filter(c => c?.dataCenterId === value)?.length || 0
      if (num > 1 && value != '') {
        return Promise.reject(new Error('告警位置重复'));
      } else {
        return Promise.resolve();
      }
    },
  })
 
 
   rules={[{ required: true, message: '请选择告警位置' }, dataCenterChecked]}

 

posted @ 2024-12-05 17:36  SimoonJia  阅读(1)  评论(0编辑  收藏  举报