表单验证
1.前端效验
data() {
var validateSort = (rule, value, callback) => {
if (value === "") {
callback(new Error("排序不能为空"));
} else if (!Number.isInteger(value) || value < 0) {
callback(new Error("排序必须是一个大于0的整数"));
} else {
callback();
}
};
return {
visible: false,
dataForm: {
brandId: 0,
name: "",
logo: "",
descript: "",
showStatus: 1,
firstLetter: "",
sort: "",
},
dataRule: {
name: [{ required: true, message: "品牌名不能为空", trigger: "blur" }],
logo: [
{ required: true, message: "品牌logo地址不能为空", trigger: "blur" },
],
descript: [
{ required: true, message: "介绍不能为空", trigger: "blur" },
],
showStatus: [
{
required: true,
message: "显示状态[0-不显示;1-显示]不能为空",
trigger: "blur",
},
],
firstLetter: [
{ required: true, message: "检索首字母不能为空", trigger: "blur" },
],
sort: [
{
validator: validateSort,
trigger: "blur",
},
],
},
};
},
- 后端效验
- BrandEntity 实体类 注解 添加效验规则,并分组
@Data
@TableName("pms_brand")
public class BrandEntity implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull(message = "修改必须指定品牌Id", groups = {UpdateGroup.class})
@Null(message = "新增不能指定品牌Id", groups = {AddGroup.class})
@TableId
private Long brandId;
@NotEmpty(message = "品牌名不能为空", groups = {AddGroup.class, UpdateGroup.class})
private String name;
@NotEmpty(message = "logo地址不能为空", groups = {AddGroup.class})
@URL(message = "logo地址不合法", groups = {AddGroup.class, UpdateGroup.class})
private String logo;
private String descript;
private Integer showStatus;
@NotEmpty(message = "检索字母不能为空", groups = {AddGroup.class})
@Pattern(regexp = "/^[a-zA-Z]$/", message = "必须以字母开头", groups = {AddGroup.class, UpdateGroup.class})
private String firstLetter;
@NotNull(message = "排序不能为空", groups = {AddGroup.class})
@Min(value = 0, message = "排序必须大于0", groups = {AddGroup.class, UpdateGroup.class})
private Integer sort;
}
@RequestMapping("/save")
public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
brandService.save(brand);
return R.ok();
}
@RequestMapping("/update")
public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
brandService.updateById(brand);
return R.ok();
}
@Slf4j
@RestControllerAdvice(basePackages = "com.rock.gulimall.product.controller")
public class GulimallExceptionControllerAdvice {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public R handleValidException(MethodArgumentNotValidException validException) {
log.error("效验异常:{}", validException.getClass());
log.error("异常信息:{}", validException.getMessage());
BindingResult bindingResult = validException.getBindingResult();
Map<String, String> map = new HashMap<>();
bindingResult.getFieldErrors().forEach((fieldError) -> map.put(fieldError.getField(), fieldError.getDefaultMessage()));
return R.error(StatusCode.VALID_EXCEPTION.getCode(), StatusCode.VALID_EXCEPTION.getMessage()).put("data", map);
}
@ExceptionHandler(value = Throwable.class)
public R handleException(Throwable throwable){
return R.error();
}
}
package com.rock.common.exception;
public enum StatusCode {
UNKNOW_EXCEPTION(10000, "系统未知异常"),
VALID_EXCEPTION(10001, "格式效验异常");
private Integer code;
private String message;
StatusCode(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}
|