[英].NET Core 2.2: Validate [required] properties only when object is not null
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
.我只希望我的
[Required]
属性在它们所在的对象不为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
.因此,例如,如果我有一个带有
Address
属性的Home
类,则Home.Address.Street
属性应该仅在Address
不为null
时才为[required]
。
In ASP.NET Core, I have a model that looks like this:在 ASP.NET Core 中,我有一个如下所示的模型:
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();
}
And the form payload looks like:表单有效负载如下所示:
homes.Index: 0
homes[0].number: 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.在 ASP.NET Core 2.2 中,
homes
列表中的第一个Home
被标记为无效,但它在 ASP.NET Core 2.1 中的工作方式与我预期的一样。
What I want is for the [Required]
attribute to only be validated if the Address
is not null
.我想要的是仅当
Address
不为null
时才验证[Required]
属性。 So a Home
can either have an Address
with a Street
or no Address
at all.因此,一个
Home
可以有一个Address
和一个Street
,也可以根本没有Address
。
Is this achievable in .NET Core 2.2?这在 .NET Core 2.2 中可以实现吗?
Note: I've included an updated example below to reproduce error.
注意:我在下面包含了一个更新的示例以重现错误。 It seems that the inclusion of
IFormFile
causes theAddress
class to initialize itself.似乎包含
IFormFile
会导致Address
类自行初始化。
{
"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"
}
I also opened an issue on GitHub for this some time ago, if anybody wants to follow up: Inclusion of IFormFile property forces the whole object to be validated.前段时间我还在 GitHub 上为此打开了一个问题,如果有人想跟进: Inclusion of IFormFile property forces the whole object to be validated。 ASP.NET Core 2.2 .
ASP.NET 核心 2.2 。
The behavior you're wanting is the behavior for null reference properties, and it has not changed in ASP.NET Core 2.2.您想要的行为是空引用属性的行为,它在 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 (ie
new Foo()
), with none of the sub properties actually defined, but that is enough to trigger validation.它可能只是一个默认实例化(即
new Foo()
),实际上没有定义任何子属性,但这足以触发验证。
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();
Or,要么,
public Foo()
{
Bar = new Bar();
}
Remove that.删除它。
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.如果引用的任何一个属性被发布,即使它本身无效,所有对类的验证都会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.