基于验证对象的自定义属性名称

发布于 2024-12-14 18:47:05 字数 1159 浏览 0 评论 0原文

我有以下课程,例如它们有点简化。

class LineInfo
{   
    Id,
    LineNumber,
    SubAccountNumer,
    MobileNumber
}

class SuspendLinesVM{
    public List<LineInfo> Lines{ get;set; }
}

我在操作中收到 SuspendLinesVM,并且所有线路都是从客户端动态创建的。表单中属于具体 LineInfo 的每个元素都具有模板“lineid{Id}_ElementName”的名称。因此,它们以如下形式来找我:

lineid0001_LineNumber

lineid0001_SubAccountNumer

lineid0001_MobileNumber

lineid0021_LineNumber

lineid0021_SubAccountNumer

lineid0021_MobileNumber

当验证时发生一些错误时,我需要一种方法来设置失败的属性,因为它是在请求中突出显示视图中的无效字段。

我留下了令我困惑的问题。

 public class LineInfoValidator: AbstractValidator<LineInfo>
    {
        public LineInfoValidator()
        {
            RuleFor(m => m.LineNumber)
                .NotEmpty().WithMessage("Line # is required").OverridePropertyName( ??? ) 
                .InclusiveBetween(1, 9999).WithMessage("Line # must be in range [1, 9999]").OverridePropertyName( ??? )
...

我需要一种方法来做 *(instance, propertyName) => 之类的事情返回 string.format('lineid_{0}_{1}', instance.Id, propertyName)*。

有什么想法吗?

I have the following classes, they are bit simplified for example.

class LineInfo
{   
    Id,
    LineNumber,
    SubAccountNumer,
    MobileNumber
}

class SuspendLinesVM{
    public List<LineInfo> Lines{ get;set; }
}

I receive SuspendLinesVM in my action and all Lines are created dynamicaly from client. Each element that belongs to concrete LineInfo in form has name with template 'lineid{Id}_ElementName'. So they comes to me in form like:

lineid0001_LineNumber

lineid0001_SubAccountNumer

lineid0001_MobileNumber

lineid0021_LineNumber

lineid0021_SubAccountNumer

lineid0021_MobileNumber

When some error occures while validation, I need a way to set the failed property as it came in request to highlight invalid fields in the view.

I left questions where I got confused.

 public class LineInfoValidator: AbstractValidator<LineInfo>
    {
        public LineInfoValidator()
        {
            RuleFor(m => m.LineNumber)
                .NotEmpty().WithMessage("Line # is required").OverridePropertyName( ??? ) 
                .InclusiveBetween(1, 9999).WithMessage("Line # must be in range [1, 9999]").OverridePropertyName( ??? )
...

I need a way to do something like *(instance, propertyName) => return string.format('lineid_{0}_{1}', instance.Id, propertyName)*.

Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-12-21 18:47:05

使用“WithState”方法解决。感谢杰里米!他的解决方案在这里 http://fluidation.codeplex.com/discussions/278892

杰里米S
2011 年 11 月 10 日下午 5:16

不幸的是,这不受支持。

属性名称仅在验证器实例化时解析一次,而不是在执行验证器时生成错误消息。在这种情况下,您需要检查正在验证的实例以生成属性名称,这实际上是不可能的 - 您能够获得的最接近的是使用 WithState 方法将某些自定义状态与失败相关联:< /p>

RuleFor(x => x.LineNumber)
    .NotEmpty()
    .WithState(instance => string.Format("lineid_{0}_LineNumber", instance.Id));

调用验证器并返回 ValidationResult 后,您可以从 ValidationFailure 的 CustomState 属性中检索该结果。

杰里米

Resolved with 'WithState' method. Thanks to Jeremy! His solution is here http://fluentvalidation.codeplex.com/discussions/278892

JeremyS
Nov 10, 2011 at 5:16 PM

Unfortunately this isn't something that is supported.

Property names are resolved only once when the validator is instantiated, as opposed to error messages which are generated when the validator is executed. In this case, you need to inspect the instance being validated to generate the property name, which isn't actually possible - the closest you'd be able to get is to use the WithState method to associate some custom state with the failure:

RuleFor(x => x.LineNumber)
    .NotEmpty()
    .WithState(instance => string.Format("lineid_{0}_LineNumber", instance.Id));

Once you invoke the validator and get back the ValidationResult, you can retrieve this from the CustomState property on the ValidationFailure.

Jeremy

热情消退 2024-12-21 18:47:05

鉴于您正在使用 FluentValidator,您应该能够在 SuspendedLinesVM 对象上设置集合验证器,如下所示:

public class SelectedLinesVMValidator : AbstractValidator<SelectedLinesVM>
{
    public SelectedLinesVMValidator()
    {
        RuleFor(x=>x.Lines).SetCollectionValidator(new LineInfoValidator());
    }
}

如果您这样做,则 根据文档,您将得到与以下内容相关的错误集合失败属性的索引。

Given that you're using FluentValidator, you should be able to set the collection validator on the SuspendedLinesVM object like so:

public class SelectedLinesVMValidator : AbstractValidator<SelectedLinesVM>
{
    public SelectedLinesVMValidator()
    {
        RuleFor(x=>x.Lines).SetCollectionValidator(new LineInfoValidator());
    }
}

If you do this, then as per the documentation you will get back a collection of errors which relate to the index of the failed property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文