fix: 校验物料名称和规格长度
This commit is contained in:
parent
bc1352b918
commit
ec293fa8b2
|
|
@ -510,6 +510,14 @@ async function handleSave() {
|
||||||
ElMessage.warning('请填写物料名称')
|
ElMessage.warning('请填写物料名称')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (Array.from(form.name).length > 200) {
|
||||||
|
ElMessage.warning('物料名称过长,请小于200字')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (Array.from(form.spec || '').length > 200) {
|
||||||
|
ElMessage.warning('物料规格过长,请小于200字')
|
||||||
|
return
|
||||||
|
}
|
||||||
saveLoading.value = true
|
saveLoading.value = true
|
||||||
try {
|
try {
|
||||||
const formData = toMaterialPayload(form)
|
const formData = toMaterialPayload(form)
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,7 @@ func (h *Handler) CreateMaterial(c *gin.Context) {
|
||||||
|
|
||||||
material, err := h.materialSvc.Create(&req, operator)
|
material, err := h.materialSvc.Create(&req, operator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrInvalidMaterialCategory) || errors.Is(err, service.ErrInvalidMaterialUnit) {
|
if errors.Is(err, service.ErrInvalidMaterialCategory) || errors.Is(err, service.ErrInvalidMaterialUnit) || errors.Is(err, service.ErrMaterialNameTooLong) || errors.Is(err, service.ErrMaterialSpecTooLong) {
|
||||||
response.BadRequest(c, err.Error())
|
response.BadRequest(c, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -371,7 +371,7 @@ func (h *Handler) UpdateMaterial(c *gin.Context) {
|
||||||
|
|
||||||
material, err := h.materialSvc.Update(orgID, id, &req, operator)
|
material, err := h.materialSvc.Update(orgID, id, &req, operator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, service.ErrInvalidMaterialCategory) || errors.Is(err, service.ErrInvalidMaterialUnit) {
|
if errors.Is(err, service.ErrInvalidMaterialCategory) || errors.Is(err, service.ErrInvalidMaterialUnit) || errors.Is(err, service.ErrMaterialNameTooLong) || errors.Is(err, service.ErrMaterialSpecTooLong) {
|
||||||
response.BadRequest(c, err.Error())
|
response.BadRequest(c, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/wmms/wmms/internal/model"
|
"github.com/wmms/wmms/internal/model"
|
||||||
"github.com/wmms/wmms/internal/repository"
|
"github.com/wmms/wmms/internal/repository"
|
||||||
|
|
@ -11,6 +12,12 @@ import (
|
||||||
|
|
||||||
var ErrInvalidMaterialCategory = errors.New("请选择有效的小类类型")
|
var ErrInvalidMaterialCategory = errors.New("请选择有效的小类类型")
|
||||||
|
|
||||||
|
var ErrMaterialNameTooLong = errors.New("物料名称过长,请小于200字")
|
||||||
|
var ErrMaterialSpecTooLong = errors.New("物料规格过长,请小于200字")
|
||||||
|
|
||||||
|
const maxMaterialNameLength = 200
|
||||||
|
const maxMaterialSpecLength = 200
|
||||||
|
|
||||||
// ErrInvalidMaterialUnit 表示物料引用的基本单位无效:要么不属于当前公司,
|
// ErrInvalidMaterialUnit 表示物料引用的基本单位无效:要么不属于当前公司,
|
||||||
// 要么已停用。创建以及"改选"单位时都要求是本公司启用中的单位。
|
// 要么已停用。创建以及"改选"单位时都要求是本公司启用中的单位。
|
||||||
var ErrInvalidMaterialUnit = errors.New("请选择有效的单位")
|
var ErrInvalidMaterialUnit = errors.New("请选择有效的单位")
|
||||||
|
|
@ -65,6 +72,12 @@ func (s *MaterialService) Create(req *model.CreateMaterialRequest, operator stri
|
||||||
if organizationID <= 0 {
|
if organizationID <= 0 {
|
||||||
return nil, ErrInvalidOrganizationID
|
return nil, ErrInvalidOrganizationID
|
||||||
}
|
}
|
||||||
|
if utf8.RuneCountInString(req.Name) > maxMaterialNameLength {
|
||||||
|
return nil, ErrMaterialNameTooLong
|
||||||
|
}
|
||||||
|
if utf8.RuneCountInString(req.Spec) > maxMaterialSpecLength {
|
||||||
|
return nil, ErrMaterialSpecTooLong
|
||||||
|
}
|
||||||
if err := s.validateCategory(req.CategoryID, organizationID); err != nil {
|
if err := s.validateCategory(req.CategoryID, organizationID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -184,6 +197,12 @@ func (s *MaterialService) Update(organizationID, id int64, req *model.UpdateMate
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if utf8.RuneCountInString(req.Name) > maxMaterialNameLength {
|
||||||
|
return nil, ErrMaterialNameTooLong
|
||||||
|
}
|
||||||
|
if utf8.RuneCountInString(req.Spec) > maxMaterialSpecLength {
|
||||||
|
return nil, ErrMaterialSpecTooLong
|
||||||
|
}
|
||||||
if req.CategoryID == nil || *req.CategoryID <= 0 {
|
if req.CategoryID == nil || *req.CategoryID <= 0 {
|
||||||
return nil, ErrInvalidMaterialCategory
|
return nil, ErrInvalidMaterialCategory
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user