From 65a1405d0fb18eb5fc899c02bb3d18ce3fc3aad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?forest=E7=BE=BD?= Date: Mon, 17 May 2021 15:18:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=95=BF=E5=BA=A6=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=A4=84=E7=90=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator/custom/handler/SizeHandler.java | 42 +++++++++++++++++++ .../custom/handler/ValidateHandlerHelper.java | 1 + 2 files changed, 43 insertions(+) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java new file mode 100644 index 0000000000..7f07a552d4 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java @@ -0,0 +1,42 @@ +package cn.iocoder.dashboard.framework.validator.custom.handler; + +import org.slf4j.helpers.MessageFormatter; + +import javax.validation.constraints.Size; + +/** + *

长度校验处理类 + * + * @author xyf + * @date 2021/5/13 + */ +public class SizeHandler implements ValidateAnnotationHandler { + + /** + * 小于等于 + */ + private static final String VALUE_LT = "长度必须大于等于{}!"; + /** + * 大于等于 + */ + private static final String VALUE_GT = "长度必须小于等于{}!"; + + @Override + public Class getAnnotation() { + return Size.class; + } + + @Override + public String validate(Size validateAnnotation, Object fieldValue) { + if (fieldValue.toString().length() > validateAnnotation.max()) { + return MessageFormatter.format(VALUE_GT, fieldValue).getMessage(); + } + + if (fieldValue.toString().length() < validateAnnotation.min()) { + return MessageFormatter.format(VALUE_LT, fieldValue).getMessage(); + } + + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java index 1d4393ab6f..c9739c84df 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java +++ b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java @@ -31,6 +31,7 @@ public class ValidateHandlerHelper { handlerSet.add(new NotBlankHandler()); handlerSet.add(new MaxHandler()); handlerSet.add(new MinHandler()); + handlerSet.add(new SizeHandler()); }