果园客户端验证——它应该怎样看/工作?

时间:2022-12-09 12:12:47

I'm attempting to use client-side validation in an admin page in Orchard. I've been successful at making it work using the techniques discussed in this question, but after doing some digging in the Orchard source and online, it seems to me that commenting out these lines

我正在尝试在Orchard的一个管理页面中使用客户端验证。我已经成功地使用了这个问题中讨论的技术,但是在深入研究了果园资源和网络之后,在我看来,我把这些代码注释掉了

// Register localized data annotations    
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());

is subverting some built-in Orchard functionality which allows for localized error strings. At this point, either having these lines in our out of OrchardStarter.cs is the only difference between validation working and not working for me.

正在颠覆一些内置的Orchard功能,该功能允许本地化错误字符串。在这一点上,这些线在我们的离开果园。cs是验证工作和不为我工作的唯一区别。

What I'm hoping for is some guidance on this, maybe from the Orchard team. If these lines have to be out in order for validation to work, why are they there in the first place? If they are there for a good reason, what am I (and others) doing wrong in our attempts to get client-side validation working? I'm happy to post code samples if needs be, although it's a pretty standard ViewModel with data annotations. Thanks.

我希望能得到一些关于这个的指导,也许来自果园团队。如果这些行必须输出以使验证有效,那么为什么它们一开始就存在呢?如果它们存在的原因是好的,那么在我们尝试让客户端验证工作时,我(和其他人)做错了什么?如果需要的话,我很乐意发布代码示例,尽管它是一个带有数据注释的标准视图模型。谢谢。

1 个解决方案

#1


2  

The lines are there to replace the DataAnnotationsModelValidatorProvider (DAMVP) with Orchard's own implementation, which allows localizing the validation messages the Orchard way. The way it does this is by replacing e.g. [Required] with [LocalizedRequired] before passing control on to the DAMVP. Note that DAMVP does get to do its job - but only after Orchard has "messed" with the attributes.

这些行将用Orchard自己的实现替换DataAnnotationsModelValidatorProvider (DAMVP),它允许以Orchard的方式本地化验证消息。它的做法是,在将控制权交给DAMVP之前,用[LocalizedRequired]替换[Required]。请注意,DAMVP确实可以完成它的工作——但只是在果园“混乱”了属性之后。

The problem is that DAMVP uses the type of the Attribute to apply client validation attributes. And now it won't find e.g. RequiredAttribute, because it's been replaced by LocalizedRequiredAttribute. So it won't know what - if any - client validation attributes it should add.

问题是DAMVP使用属性的类型来应用客户端验证属性。现在它找不到,例如,RequiredAttribute,因为它已经被LocalizedRequiredAttribute取代了。因此它不知道应该添加什么(如果有的话)客户端验证属性。

So, out-commenting the lines will make you lose Orchard's localization. Leaving them in will make you lose client validation.

因此,对代码行进行注释会使您失去Orchard的本地化。保留它们将使您失去客户验证。

One workaround that might work (haven't looked enough through Orchard's code, and haven't the means to test at the moment) would be to make DAMVP aware of Orchard's Localized attributes and what to do with them.

一个可行的解决方案是让DAMVP了解Orchard的本地化属性以及如何使用它们。

DAMVP has a static RegisterAdapter() method for the purpose of adding new client rules for attributes. It takes the type of the attribute and the type of the Client-side adapter (the class that takes care of adding client attributes) to use.

DAMVP有一个用于为属性添加新客户端规则的静态RegisterAdapter()方法。它采用了属性的类型和客户端适配器的类型(负责添加客户端属性的类)。

So, something like the following might work:

所以,以下这样的方法可能会奏效:

In OrchardStarter.cs:

在OrchardStarter.cs:

// Leave the LocalizedModelValidatorProvider lines uncommented/intact

// These are the four attributes Orchard replaces. Register the standard
// client adapters for them:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRegularExpressionAttribute), 
    typeof(RegularExpressionAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRequiredAttribute), 
    typeof(RequiredAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRangeAttribute), 
    typeof(RangeAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedStringLengthAttribute), 
    typeof(StringLengthAttributeAdapter)
);

As for the official word, it would seem this hasn't worked since localized validation was introduced in 1.3, and the impact is considered low: http://orchard.codeplex.com/workitem/18269

至于官方的说法,自1.3引入本地化验证以来,似乎还没有起作用,其影响被认为很低:http://orchard.codeplex.com/workitem/18269

So, at the moment, it appears the official answer to the question title is, "it shouldn't".

因此,目前看来,这个问题的官方答案是“不应该”。

#1


2  

The lines are there to replace the DataAnnotationsModelValidatorProvider (DAMVP) with Orchard's own implementation, which allows localizing the validation messages the Orchard way. The way it does this is by replacing e.g. [Required] with [LocalizedRequired] before passing control on to the DAMVP. Note that DAMVP does get to do its job - but only after Orchard has "messed" with the attributes.

这些行将用Orchard自己的实现替换DataAnnotationsModelValidatorProvider (DAMVP),它允许以Orchard的方式本地化验证消息。它的做法是,在将控制权交给DAMVP之前,用[LocalizedRequired]替换[Required]。请注意,DAMVP确实可以完成它的工作——但只是在果园“混乱”了属性之后。

The problem is that DAMVP uses the type of the Attribute to apply client validation attributes. And now it won't find e.g. RequiredAttribute, because it's been replaced by LocalizedRequiredAttribute. So it won't know what - if any - client validation attributes it should add.

问题是DAMVP使用属性的类型来应用客户端验证属性。现在它找不到,例如,RequiredAttribute,因为它已经被LocalizedRequiredAttribute取代了。因此它不知道应该添加什么(如果有的话)客户端验证属性。

So, out-commenting the lines will make you lose Orchard's localization. Leaving them in will make you lose client validation.

因此,对代码行进行注释会使您失去Orchard的本地化。保留它们将使您失去客户验证。

One workaround that might work (haven't looked enough through Orchard's code, and haven't the means to test at the moment) would be to make DAMVP aware of Orchard's Localized attributes and what to do with them.

一个可行的解决方案是让DAMVP了解Orchard的本地化属性以及如何使用它们。

DAMVP has a static RegisterAdapter() method for the purpose of adding new client rules for attributes. It takes the type of the attribute and the type of the Client-side adapter (the class that takes care of adding client attributes) to use.

DAMVP有一个用于为属性添加新客户端规则的静态RegisterAdapter()方法。它采用了属性的类型和客户端适配器的类型(负责添加客户端属性的类)。

So, something like the following might work:

所以,以下这样的方法可能会奏效:

In OrchardStarter.cs:

在OrchardStarter.cs:

// Leave the LocalizedModelValidatorProvider lines uncommented/intact

// These are the four attributes Orchard replaces. Register the standard
// client adapters for them:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRegularExpressionAttribute), 
    typeof(RegularExpressionAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRequiredAttribute), 
    typeof(RequiredAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRangeAttribute), 
    typeof(RangeAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedStringLengthAttribute), 
    typeof(StringLengthAttributeAdapter)
);

As for the official word, it would seem this hasn't worked since localized validation was introduced in 1.3, and the impact is considered low: http://orchard.codeplex.com/workitem/18269

至于官方的说法,自1.3引入本地化验证以来,似乎还没有起作用,其影响被认为很低:http://orchard.codeplex.com/workitem/18269

So, at the moment, it appears the official answer to the question title is, "it shouldn't".

因此,目前看来,这个问题的官方答案是“不应该”。