简体   繁体   English

使用数据注释进行验证不起作用

[英]Validation with data annotations does not work

I'm develop a REST API with a customer purchase model.我正在开发一个带有客户购买模型的 REST API。 I have applied some validations, but I seen that validations on the properties with decimal type does not work, however, tha validatios on the properties with type string work successfull.我已经应用了一些验证,但是我看到对十进制类型的属性的验证不起作用,但是,对字符串类型的属性的验证成功工作。

The behavior I have is if not add the property, for example, total, tha validation does not applied and this property take the default value for decimal types, that is 0.0我的行为是如果不添加属性,例如,总计,不应用验证,并且此属性采用十进制类型的默认值,即 0.0

That is, I have the following model:也就是说,我有以下模型:

public class CustomerPurchase : BaseEntity
    {
        [Required(ErrorMessage = "PurchaseFolioRequired")]
        public string Folio { get; set; }

        [Required(ErrorMessage = "PurchaseIvaRequired")]
[Range(typeof(Decimal), "0", "1000000000000000000", ErrorMessage = "PurchaseInvalidVat")]
        public decimal Iva { get; set; }

        [Required(ErrorMessage = "PurchaseSubtotalRequired")]
[Range(typeof(Decimal), "0", "1000000000000000000", ErrorMessage = "PurchaseInvalidSubtotal")]
        public decimal Subtotal { get; set; }

        [Required(ErrorMessage = "PurchaseTotalRequired")]
[Range(typeof(Decimal), "0", "1000000000000000000", ErrorMessage = "PurchaseInvalidTotal")]
        public decimal Total { get; set; }

        [Required(ErrorMessage = "PurchaseTotalLettersRequired")]
        public string TotalLetters { get; set; }

        [Required(ErrorMessage = "PurchaseEmployeeRequired")]
        [BsonRepresentation(BsonType.ObjectId)]
        public string UserId { get; set; }

        [Required(ErrorMessage = "PurchaseProductRequired")]
        [MinLength(1, ErrorMessage = "PurchaseProductRequired")]
        public ProductSold[] Products { get; set; }

        [Required(ErrorMessage = "PurchasePaymentRequired")]
        [MinLength(1, ErrorMessage = "PurchasePaymentRequired")]
        public Payment[] Payments { get; set; }

        [Required(ErrorMessage = "PurchaseClientRequired")]
        public Client Client { get; set; }
    }

This application is wrong?, or there is a way to applied strictly this validation.这个应用程序是错误的?,或者有一种方法可以严格应用这个验证。

Thanks for reading谢谢阅读

decimal is a value type , so it always has a default value - 0 in this case, so it always passes your validation. decimal值类型,因此它始终具有默认值 - 在这种情况下为0 ,因此它始终通过您的验证。 So you can just make them nullable :所以你可以让它们为

public class CustomerPurchase : BaseEntity
{
    ....
    [Required(ErrorMessage = "PurchaseIvaRequired")]
    [Range(typeof(Decimal), "0", "1000000000000000000", ErrorMessage = "PurchaseInvalidVat")]
    public decimal? Iva { get; set; }
    ...
}

Docs for RequiredAttribute are also pretty clear about it: RequiredAttribute 文档也很清楚:

The RequiredAttribute attribute specifies that when a field on a form is validated, the field must contain a value. RequiredAttribute属性指定在验证表单上的字段时,该字段必须包含一个值。 A validation exception is raised if the property is null , contains an empty string (""), or contains only white-space characters.如果属性为null 、包含空字符串 ("") 或仅包含空白字符,则会引发验证异常。

You could code it like this:你可以这样编码:

[Range(1, 100)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }

From: Part 8, add validation to an ASP.NET Core Razor Page来自: 第 8 部分,向 ASP.NET Core Razor 页面添加验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM