func (cOut *ConsumableOut) Validate(db *gorm.DB) {
	if cOut.ReportItemID == 0 {
		db.AddError(validations.NewError(cOut, "ReportItemID", "消耗品不能为空"))
	}
	if cOut.ToWhomID == 0 {
		db.AddError(validations.NewError(cOut, "ToWhomID", "消耗品使用人不能为空"))
	}
	if cOut.Quantity <= 0 {
		db.AddError(validations.NewError(cOut, "Quantity", "带出消耗品的数量要大于0"))
	}
	if cOut.ByWhomID == 0 {
		db.AddError(validations.NewError(cOut, "ByWhomID", "请选择操作员"))
	}

	from, to, d, _, _, err := fromToDevice(cOut.ReportItemID, cOut.ToWhomID, "Employee")
	if err != nil {
		db.AddError(validations.NewError(cOut, "FromReportItemID", err.Error()))
		return
	}

	cOut.DeviceName = d.Name
	cOut.ToWhomName = to.HolderName()
	cOut.WarehouseName = from.HolderName()

	byWhom, _ := holderByIDType(cOut.ByWhomID, "Employee")
	cOut.ByWhomName = byWhom.HolderName()

}
func (dOut *DeviceOut) Validate(db *gorm.DB) {
	if dOut.FromReportItemID == 0 {
		db.AddError(validations.NewError(dOut, "FromReportItemID", "带出设备不能为空"))
	}
	if dOut.ToWhomID == 0 {
		db.AddError(validations.NewError(dOut, "ToWhomID", "设备带出人不能为空"))
	}
	if dOut.Quantity <= 0 {
		db.AddError(validations.NewError(dOut, "Quantity", "带出设备的数量要大于0"))
	}
	if dOut.ByWhomID == 0 {
		db.AddError(validations.NewError(dOut, "ByWhomID", "请选择操作员"))
	}

	from, to, d, _, _, err := fromToDevice(dOut.FromReportItemID, dOut.ToWhomID, "Employee")
	if err != nil {
		db.AddError(validations.NewError(dOut, "FromReportItemID", err.Error()))
		return
	}

	dOut.DeviceName = d.Name
	dOut.ToWhomName = to.HolderName()
	dOut.FromWarehouseName = from.HolderName()

	byWhom, _ := holderByIDType(dOut.ByWhomID, "Employee")
	dOut.ByWhomName = byWhom.HolderName()

}
func (dIn *ClientDeviceCheckIn) Validate(db *gorm.DB) {
	if dIn.FromReportItemID == 0 {
		db.AddError(validations.NewError(dIn, "FromReportItemID", "送检的设备不能为空"))
	}
	if dIn.ToWarehouseID == 0 {
		db.AddError(validations.NewError(dIn, "ToWhomID", "送检设备还回仓库不能为空"))
	}
	if dIn.Quantity <= 0 {
		db.AddError(validations.NewError(dIn, "Quantity", "还回送检设备的数量要大于0"))
	}
	if dIn.ByWhomID == 0 {
		db.AddError(validations.NewError(dIn, "ByWhomID", "请选择操作员"))
	}

	from, to, _, cdIn, _, err := fromToDevice(dIn.FromReportItemID, dIn.ToWarehouseID, "Warehouse")
	if err != nil {
		db.AddError(validations.NewError(dIn, "FromReportItemID", err.Error()))
		return
	}

	dIn.DeviceName = cdIn.DeviceName
	dIn.ClientName = cdIn.ClientName
	dIn.ToWarehouseName = to.HolderName()
	dIn.FromDeviceCheckCompanyName = from.HolderName()

	byWhom, _ := holderByIDType(dIn.ByWhomID, "Employee")
	dIn.ByWhomName = byWhom.HolderName()

}
func (dOut *ClientDeviceCheckOut) Validate(db *gorm.DB) {
	if dOut.FromReportItemID == 0 {
		db.AddError(validations.NewError(dOut, "FromReportItemID", "送检设备不能为空"))
	}
	if dOut.ToDeviceCheckCompanyID == 0 {
		db.AddError(validations.NewError(dOut, "ToDeviceCheckCompanyID", "送检公司不能为空"))
	}
	if dOut.Quantity <= 0 {
		db.AddError(validations.NewError(dOut, "Quantity", "送检设备的数量要大于0"))
	}
	if dOut.ByWhomID == 0 {
		db.AddError(validations.NewError(dOut, "ByWhomID", "请选择操作员"))
	}

	from, to, _, cdIn, _, err := fromToDevice(dOut.FromReportItemID, dOut.ToDeviceCheckCompanyID, "DeviceCheckCompany")
	if err != nil {
		db.AddError(validations.NewError(dOut, "FromReportItemID", err.Error()))
		return
	}

	dOut.DeviceName = cdIn.DeviceName
	dOut.ClientName = cdIn.ClientName
	dOut.ToDeviceCheckCompanyName = to.HolderName()
	dOut.FromWarehouseName = from.HolderName()

	byWhom, _ := holderByIDType(dOut.ByWhomID, "Employee")
	dOut.ByWhomName = byWhom.HolderName()

}
Exemple #5
0
func (color Color) Validate(db *gorm.DB) {
	if strings.TrimSpace(color.Name) == "" {
		db.AddError(validations.NewError(color, "Name", "Name can not be empty"))
	}

	if strings.TrimSpace(color.Code) == "" {
		db.AddError(validations.NewError(color, "Code", "Code can not be empty"))
	}
}
Exemple #6
0
func (size Size) Validate(db *gorm.DB) {
	if strings.TrimSpace(size.Name) == "" {
		db.AddError(validations.NewError(size, "Name", "Name can not be empty"))
	}

	if strings.TrimSpace(size.Code) == "" {
		db.AddError(validations.NewError(size, "Code", "Code can not be empty"))
	}
}
Exemple #7
0
func (product Product) Validate(db *gorm.DB) {
	if strings.TrimSpace(product.Name) == "" {
		db.AddError(validations.NewError(product, "Name", "Name can not be empty"))
	}

	if strings.TrimSpace(product.Code) == "" {
		db.AddError(validations.NewError(product, "Code", "Code can not be empty"))
	}
}
Exemple #8
0
func (user *User) Validate(db *gorm.DB) {
	govalidator.CustomTypeTagMap.Set("uniqEmail", govalidator.CustomTypeValidator(func(email interface{}, context interface{}) bool {
		var count int
		if db.Model(&User{}).Where("email = ?", email).Count(&count); count == 0 || email == "" {
			return true
		}
		return false
	}))
	if user.Name == "invalid" {
		db.AddError(validations.NewError(user, "Name", "invalid user name"))
	}
}
func (cdOut *ClientDeviceOut) Validate(db *gorm.DB) {

	if cdOut.ReportItemID == 0 {
		db.AddError(validations.NewError(cdOut, "ReportItemID", "选择收入过的客户设备"))
	}
	if cdOut.ByWhomID == 0 {
		db.AddError(validations.NewError(cdOut, "ByWhomID", "请选择操作员"))
	}

	_, _, _, cdIn, ri, err := fromToDevice(cdOut.ReportItemID, 0, "Employee")
	if err != nil {
		db.AddError(validations.NewError(cdOut, "ReportItemID", err.Error()))
		return
	}

	if int(cdIn.Quantity) != ri.Count {
		db.AddError(validations.NewError(cdOut, "ReportItemID", "该客户设备有送检中设备,不能还回。"))
		return
	}

	cdOut.DeviceName = cdIn.DeviceName
	cdOut.ClientName = cdIn.ClientName
	cdOut.Quantity = cdIn.Quantity
	cdOut.WarehouseName = cdIn.WarehouseName
}
Exemple #10
0
// Validate does some validation to be able to store the record.
func (u *Registry) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Name, "1", "255") {
		db.AddError(fmt.Errorf("Name should be longer than 1 and shorter than 255"))
	}

	if u.Name != "" {
		notFound := db.Where(
			"name = ?",
			u.Name,
		).Not(
			"id",
			u.ID,
		).First(
			&Registry{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Name is already present"))
		}
	}

	if !govalidator.StringLength(u.Host, "1", "255") {
		db.AddError(fmt.Errorf("Host should be longer than 1 and shorter than 255"))
	}

	if !govalidator.IsRequestURL(u.Host) {
		db.AddError(fmt.Errorf(
			"Host must be a valid URL",
		))
	}

	if u.Host != "" {
		notFound := db.Where(
			"host = ?",
			u.Host,
		).Not(
			"id",
			u.ID,
		).First(
			&Registry{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Host is already present"))
		}
	}
}
Exemple #11
0
// Validate does some validation to be able to store the record.
func (u *Team) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Name, "1", "255") {
		db.AddError(fmt.Errorf("Name should be longer than 1 and shorter than 255"))
	}

	if u.Name != "" {
		notFound := db.Where(
			"name = ?",
			u.Name,
		).Not(
			"id",
			u.ID,
		).First(
			&Team{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Name is already present"))
		}
	}
}
func (cdIn *ClientDeviceIn) Validate(db *gorm.DB) {
	if len(strings.TrimSpace(cdIn.DeviceName)) == 0 {
		db.AddError(validations.NewError(cdIn, "DeviceName", "收入设备名不能为空"))
	}
	if len(strings.TrimSpace(cdIn.ClientName)) == 0 {
		db.AddError(validations.NewError(cdIn, "ClientName", "收入客户名不能为空"))
	}
	if cdIn.WarehouseID == 0 {
		db.AddError(validations.NewError(cdIn, "WharehouseID", "收入到的仓库不能为空"))
	}
	if cdIn.Quantity <= 0 {
		db.AddError(validations.NewError(cdIn, "Quantity", "收入设备的数量要大于0"))
	}
	if cdIn.ByWhomID == 0 {
		db.AddError(validations.NewError(cdIn, "ByWhomID", "请选择操作员"))
	}

	wh, _ := holderByIDType(cdIn.WarehouseID, "Warehouse")
	cdIn.WarehouseName = wh.HolderName()

	byWhom, _ := holderByIDType(cdIn.ByWhomID, "Employee")
	cdIn.ByWhomName = byWhom.HolderName()
}
func (device *Device) Validate(db *gorm.DB) {
	var deviceInDb Device
	db.Where("code = ?", device.Code).First(&deviceInDb)

	if deviceInDb.ID != 0 && deviceInDb.ID != device.ID {
		db.AddError(validations.NewError(device, "Code", "代码已经存在了,不能重复"))
	}

	if device.Name == "" {
		db.AddError(validations.NewError(device, "Name", "设备名称不能为空"))
	}

	catName := ""
	for _, dc := range DeviceCategories {
		if fmt.Sprintf("%d", device.CategoryID) == dc[0] {
			catName = dc[1]
			break
		}
	}
	// panic(fmt.Sprintf("%d", d.CategoryID) + catName)
	device.CategoryName = catName

}
func (cIn *ConsumableIn) Validate(db *gorm.DB) {
	if cIn.ReportItemID == 0 {
		db.AddError(validations.NewError(cIn, "ReportItemID", "购买的消耗品不能为空"))
	}
	if cIn.Quantity <= 0 {
		db.AddError(validations.NewError(cIn, "Quantity", "购买的数量要大于0"))
	}
	if cIn.ByWhomID == 0 {
		db.AddError(validations.NewError(cIn, "ByWhomID", "请选择操作员"))
	}

	from, _, d, _, _, err := fromToDevice(cIn.ReportItemID, 0, "Employee")
	if err != nil {
		db.AddError(validations.NewError(cIn, "ReportItemID", err.Error()))
		return
	}

	cIn.DeviceName = d.Name
	cIn.WarehouseName = from.HolderName()
	byWhom, _ := holderByIDType(cIn.ByWhomID, "Employee")
	cIn.ByWhomName = byWhom.HolderName()

}
Exemple #15
0
// Validate does some validation to be able to store the record.
func (u *User) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Username, "2", "255") {
		db.AddError(fmt.Errorf("Username should be longer than 2 and shorter than 255"))
	}

	if u.Username != "" {
		notFound := db.Where(
			"username = ?",
			u.Username,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Username is already present"))
		}
	}

	if u.Hash != "" {
		notFound := db.Where(
			"hash = ?",
			u.Hash,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Hash is already present"))
		}
	}

	if !govalidator.IsEmail(u.Email) {
		db.AddError(fmt.Errorf(
			"Email must be a valid email address",
		))
	}

	if u.Email != "" {
		normalized, _ := govalidator.NormalizeEmail(
			u.Email,
		)

		notFound := db.Where(
			"email = ?",
			normalized,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Email is already present"))
		}
	}

	if db.NewRecord(u) {
		if !govalidator.StringLength(u.Password, "5", "255") {
			db.AddError(fmt.Errorf("Password should be longer than 5 and shorter than 255"))
		}
	}
}
Exemple #16
0
func (category Category) Validate(db *gorm.DB) {
	if strings.TrimSpace(category.Name) == "" {
		db.AddError(validations.NewError(category, "Name", "Name can not be empty"))
	}
}
Exemple #17
0
func (address *Address) Validate(db *gorm.DB) {
	if address.Address == "invalid" {
		db.AddError(validations.NewError(address, "Address", "invalid address"))
	}
}
Exemple #18
0
func (card *CreditCard) Validate(db *gorm.DB) {
	if !regexp.MustCompile("^(\\d){13,16}$").MatchString(card.Number) {
		db.AddError(validations.NewError(card, "Number", "invalid card number"))
	}
}
Exemple #19
0
func (company *Company) Validate(db *gorm.DB) {
	if company.Name == "invalid" {
		db.AddError(errors.New("invalid company name"))
	}
}
Exemple #20
0
func (user *User) Validate(db *gorm.DB) {
	if user.Name == "invalid" {
		db.AddError(validations.NewError(user, "Name", "invalid user name"))
	}
}
Exemple #21
0
func (productImage ProductImage) Validate(db *gorm.DB) {
	if strings.TrimSpace(productImage.Title) == "" {
		db.AddError(validations.NewError(productImage, "Title", "Tile can not be empty"))
	}
}