.NET Core 2.2:仅当object不为null时才验证[必需]属性 [英] .NET Core 2.2: Validate [required] properties only when object is not null

查看:209
本文介绍了.NET Core 2.2:仅当object不为null时才验证[必需]属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有嵌套对象的复杂模型.我只希望当[Required]属性不在其上的对象不是null时进行验证.因此,例如,如果我有一个具有Address属性的Home类,则如果Address不是null,则Home.Address.Street属性应仅是[required].

I have a complex model with nested objects. I only want my [Required] attributes to be validated when the object they are on is not null. So, for example, if I have a Home class with an Address property, the Home.Address.Street property should only be [required] if the Address is not null.

在ASP.NET Core中,我有一个看起来像这样的模型:

In ASP.NET Core, I have a model that looks like this:

public class Home
{
    [Required]
    public int Number {get;set;}
    public Address Address {get;set;} // This is not required
}

public class Address
{
   [Required]
   public string Street {get;set;}
   public IFormFile Picture {get;set;}

}

在控制器中,我有一个如下所示的操作方法:

In a controller, I have an action method that looks like this:

[HttpPost]
public string AddHomes([FromForm]List<Home> homes) 
{
    if(!ModelState.IsValid)
    {
        return BadRequest();
    }
    // Do some saving
    return Ok();  
}

表单有效载荷如下:

homes.Index: 0
homes[0].number: 1

在ASP.NET Core 2.2中,homes列表中的第一个Home被标记为无效,但是它在ASP.NET Core 2.1中可以正常工作.

In ASP.NET Core 2.2, the first Home in the homes list is marked as invalid, but it was working as I would expect in ASP.NET Core 2.1.

我想要的是[Required]属性仅在Address不是null时才得到验证.因此,Home可以 拥有一个Address带有Street 或完全没有Address的.

What I want is for the [Required] attribute to only be validated if the Address is not null. So a Home can either have an Address with a Street or no Address at all.

.NET Core 2.2中可以实现吗?

Is this achievable in .NET Core 2.2?

注意:我在下面提供了一个更新的示例,以重现错误. 似乎包含IFormFile会导致Address类自行初始化.

Note: I've included an updated example below to reproduce error. It seems that the inclusion of IFormFile causes the Address class to initialize itself.

{
    "errors": {
        "homes[0].Address.Street": [
            "The Street field is required."
        ]
    },
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "80000009-0003-ff00-b63f-84710c7967bb"
}

前段时间,如果有人想跟进,我也在GitHum上发布了一个问题:包含IFormFile属性将强制验证整个对象. ASP.NET Core 2.2 .

I also opened an issue on GitHum for this some time ago, if anybody wants to follow up: Inclusion of IFormFile property forces the whole object to be validated. ASP.NET Core 2.2.

推荐答案

您想要的行为空引用属性的行为,并且在ASP.NET Core 2.2中未更改.仅当引用本身为非null时,才会验证所引用类的属性.如果这对您不起作用,那么唯一的结论就是该引用属性确实具有值.它可能只是默认的实例化(即new Foo()),没有实际定义任何子属性,但这足以触发验证.

The behavior you're wanting is the behavior for null reference properties, and it has not changed in ASP.NET Core 2.2. The properties on the referenced class are only validated if the reference itself is non-null. If this isn't working for you, then the only conclusion is that this reference property does have a value. It might just be a default instantiation (i.e. new Foo()), with none of the sub properties actually defined, but that is enough to trigger validation.

首先,请确保未设置属性的默认值,例如,否则不要通过构造函数为其提供默认值.换句话说,如果您有类似的东西:

First, ensure that you are not setting a default value for the property, or otherwise providing it a default through the contructor, for example. In other words, if you have something like:

public Bar Bar { get; set; } = new Bar();

或者,

public Foo()
{
    Bar = new Bar();
}

删除.

此外,请注意,如果为该参考属性发布了任何内容,那么一切都会发挥作用.即使您只有一些隐藏的属性,例如:

Also, realize that if anything is posted for that reference property, then everything comes into play. Even if you just have some hidden property like:

<input type="hidden" asp-for="Bar.Id" />

如果发布了引用中的任何一个属性,即使该属性本身无效,则将对类进行所有验证.

If any one property on the reference is posted, even if it, itself, is not valid, all validation on the class will come into play.

这篇关于.NET Core 2.2:仅当object不为null时才验证[必需]属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆