mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-11-07 01:33:44 +08:00
@@ -9,7 +9,7 @@ import static java.lang.annotation.ElementType.*;
|
|||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义枚举校验注解
|
* 自定义枚举校验
|
||||||
*
|
*
|
||||||
* @author 秋辞未寒
|
* @author 秋辞未寒
|
||||||
* @date 2024-12-09
|
* @date 2024-12-09
|
||||||
@@ -27,9 +27,10 @@ public @interface EnumPattern {
|
|||||||
Class<? extends Enum> type() default Enum.class;
|
Class<? extends Enum> type() default Enum.class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 枚举类型校验值判断方法
|
* 枚举类型校验值字段名称
|
||||||
|
* TODO 需确保该字段实现了 getter 方法
|
||||||
*/
|
*/
|
||||||
String method() default "getCode";
|
String fieldName() default "code";
|
||||||
|
|
||||||
String message() default "输入值不在枚举范围内";
|
String message() default "输入值不在枚举范围内";
|
||||||
|
|
||||||
|
|||||||
@@ -28,10 +28,10 @@ public class EnumPatternValidator implements ConstraintValidator<EnumPattern, St
|
|||||||
try {
|
try {
|
||||||
if (StringUtils.isNotBlank(value)) {
|
if (StringUtils.isNotBlank(value)) {
|
||||||
Class<?> type = annotation.type();
|
Class<?> type = annotation.type();
|
||||||
|
String fieldName = annotation.fieldName();
|
||||||
Object[] enumConstants = type.getEnumConstants();
|
Object[] enumConstants = type.getEnumConstants();
|
||||||
Method method = ReflectUtils.getMethod(type, annotation.method());
|
|
||||||
for (Object e : enumConstants) {
|
for (Object e : enumConstants) {
|
||||||
if (value.equals(method.invoke(e))) {
|
if (value.equals(ReflectUtils.invokeGetter(e, fieldName))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user