可以用somone帮助我更好地理解本教程吗?

时间:2021-07-14 19:00:13

I am trying to go through this tutorial tutorial . This tutorial seems to do everything that I been looking for but I am having trouble trying to understand it.

我正在尝试阅读本教程。本教程似乎完成了我一直在寻找的所有内容,但我无法理解它。

Maybe it is because I don't know enough of Xval, jquery.validate.

也许是因为我不太了解Xval,jquery.validate。

1st He seems to be using a typed view: RemoteValidation.Models.User. What happens if I want to pass some other data, how would I get by this? Do I have to make another class store the User model and the other stuff I want in it?

他似乎使用了一个类型视图:RemoteValidation.Models.User。如果我想传递一些其他数据会发生什么,我将如何得到这个?我是否必须创建另一个类存储用户模型以及我想要的其他内容?

2nd

I get confused a lot through the article since this is suppost to be like client and server side validtion but then he adds lines like

我在文章中感到很困惑,因为这可能是客户端和服务器端的验证,但随后他添加了类似的行

“//add optional regEx validator to minimize ajax requests”

“//添加可选的regEx验证器以最小化ajax请求”

So to me this rasies the question of I thought they already had this in place:

所以对我来说这个问题我觉得他们已经有了这个问题:

[RegularExpression(EmailRegEx, ErrorMessage = "Invalid e-mail address.")]”

Then he has this

然后他有这个

“For example, all you’d need to implement for server AND remote client-side validation of password strength is this: ”

“例如,您需要为服务器和远程客户端验证密码强度而实现的是:”

public class IsSafePasswordAttribute : RemotePropertyValidator
{

public IsSafePasswordAttribute()
{
//perform some simple password strength check with a regular expression
//on the client side first
ClientSideRegEx = “.{8,20}”;
}

protected override bool PropertyValid(object value)
{
//Insert more elaborate server-side / remote client side password checking
// logic and return result here…
}

So then if you read the comments it seems like the constructor is for client side and this PropertyValid is for server side?

那么,如果您阅读注释,似乎构造函数是为客户端而这个PropertyValid是服务器端?

Is this correct? What happens if I just want the same thing for both client and server side?

它是否正确?如果我只想为客户端和服务器端同样的事情会发生什么?

But then he has this

但他有这个

protected override bool PropertyValid(object value)
{
return (string)value != “adrian@lobstersoft.com”;
}

This code gets run like if it was just on the client side.

这个代码就像它只是在客户端一样运行。

I don't get

我没有

ClientSideRegEx = “.{8,20}”;

First this now really tells me that it is for only the ClientSide so that means I would have to write the same code for the server side? I thought that was the whole purpose of this to not write the same code twice?

首先,这现在真的告诉我它只适用于ClientSide,这意味着我必须为服务器端编写相同的代码?我认为这样做的全部目的是不要两次编写相同的代码?

Also don't they have one of these "[]" for range like they have for required? I would think that would a very basic one so what would you need to make your own?

也不是他们有这些“[]”中的一个像他们所要求的范围?我认为这将是一个非常基本的,所以你需要自己做什么?

I also don't understand how he figured out that's how it needs to be inputed. Like is there so documentation I can look at. I know it is a regex expression but I am wondering about other validation and stuff.

我也不明白他是如何弄清楚它是如何被输入的。就像有这样的文档,我可以看看。我知道这是一个正则表达式,但我想知道其他验证和东西。

[Files in the Demo]

[演示中的文件]

Then overall I don’t know what to I should be changing and what I can leave alone. Like if I start a new project and start doing my validation do I include these files?

然后整体而言我不知道应该改变什么以及我可以独自留下什么。就像我开始一个新项目并开始进行验证一样,我是否包含这些文件?

DataAnnotationsModelBinder.cs ExtensionMethods.cs RemoteValidators.cs

DataAnnotationsModelBinder.cs ExtensionMethods.cs RemoteValidators.cs

Plus of course the xval files. Do I need to change any of these .cs files? Or can I use them as is? Like do I just start inheriting the “RemotePropertyValidator” and just start typing or do I have to do some other changes?

当然还有xval文件。我是否需要更改任何这些.cs文件?或者我可以按原样使用它们吗?就像我刚刚开始继承“RemotePropertyValidator”并开始输入或我是否必须做一些其他更改?

Then on a side note do they still make Xval I was looking on the site and its only 0.8(and only beta still) right now and has not been updated in a couple months. I posted this question "if they where still being developed" and no response yet.

然后在侧面说明他们仍然使Xval我在网站上看,它现在只有0.8(并且只有beta版),并且在几个月内没有更新。我发布了这个问题“如果他们还在开发中”并且还没有回复。

That gives me a uneasy feeling.

这给了我一种不安的感觉。

Thanks

P.S I am open of using other stuff then xval if it can do the same thing(easier). As long as it hooks up with jquery and asp.net mvc.

P.S我开放使用其他东西然后xval如果它可以做同样的事情(更容易)。只要它与jquery和asp.net mvc挂钩。

2 个解决方案

#1


I'm only going to answer a few of the trickier questions you've asked and address the larger principles. Hopefully this will help you a bit.

我只会回答你提出的一些棘手的问题,并解决更大的原则。希望这会对你有所帮助。

1) No, you don't have to use strongly typed views. I never do. Personally, I prefer the MVCContrib strongly typed viewdata.get (viewdata.get("user");).

1)不,您不必使用强类型视图。我从来没有做。就个人而言,我更喜欢MVCContrib强类型的viewdata.get(viewdata.get(“user”);)。

2) Yes, the xVal (and other similar libraries) help you from doing the same stuff on the server side and client side. However... you have to set up the validation rules in the first place.

2)是的,xVal(以及其他类似的库)可以帮助您在服务器端和客户端执行相同的操作。但是......您必须首先设置验证规则。

For things like [Required] or [StringLength] this has already been done for you. But there'll always be the possibility that you need more complicated validation. And in this case, you need to define the routines separately (different languages, different elements, varying access to data (databases on the server)).

对于[必需]或[StringLength]这样的事情已经为你完成了。但是总有可能需要更复杂的验证。在这种情况下,您需要单独定义例程(不同的语言,不同的元素,不同的数据访问权限(服务器上的数据库))。

The IsPasswordSafe attribute is a good example. The constructor just sets the javascript regular expression (in this case ".{8,20}" returns true if the value is between 8 and 20 characters) -- a VERY simple check. My assumption is that after that string is set in the constructor, the functions responsible for creating the clientside javascript will read it. On the serverside (the PropertyValid() method), you can do more intensive things -- check a dictionary, check that user's previous passwords, etc -- that could NOT be done locally.

IsPasswordSafe属性就是一个很好的例子。构造函数只是设置javascript正则表达式(在这种情况下“。{8,20}”如果值在8到20个字符之间则返回true) - 非常简单的检查。我的假设是在构造函数中设置了该字符串后,负责创建clientside javascript的函数将读取它。在服务器端(PropertyValid()方法),你可以做更多密集的事情 - 检查字典,检查用户以前的密码等 - 这是不能在本地完成的。

I suggest you re-read the posting with the above in mind, and play around a bit, and hopefully everything will become clearer.

我建议你重新阅读上面记载的内容,然后玩一下,希望一切都会变得更加清晰。

On a personal note, I've just started using xVal and I'm pretty happy with it. I haven't gotten into custom validation (maybe you should start playing with the included DataAnnotations stuff). But one of the points of xval is that you don't need to know anything about jquery.validate. Just know that the server side and client side don't always have to be -- and sometimes can't be -- in sync, validation-wise.

就个人而言,我刚刚开始使用xVal,我对它非常满意。我没有进入自定义验证(也许你应该开始使用附带的DataAnnotations东西)。但是xval的一个要点是你不需要了解jquery.validate的任何信息。只要知道服务器端和客户端并不总是必须 - 有时不能 - 同步,验证方面。

James

#2


In my understanding, the article describes two different ways of client validation:

根据我的理解,本文描述了两种不同的客户端验证方法:

  1. Remote client validation, where client sends an AJAX request to server, asking server to validate. This is different from pure server side validation since it does not require page reload. The article shows how to perform remote client validation without any additional code.

    远程客户端验证,客户端向服务器发送AJAX请求,要求服务器验证。这与纯服务器端验证不同,因为它不需要页面重新加载。本文介绍如何在没有任何其他代码的情况下执行远程客户端验证

  2. Pure client validation, this is what ClientSideRegEx is used for -- in this case there is no server request at all, but more code is required.

    纯客户端验证,这就是ClientSideRegEx用于的 - 在这种情况下根本没有服务器请求,但需要更多代码。

As for RemoteValidation.Models.User -- you can pass any other class, as long as it uses annotation attributes on its properties.

对于RemoteValidation.Models.User - 您可以传递任何其他类,只要它在其属性上使用注释属性即可。

#1


I'm only going to answer a few of the trickier questions you've asked and address the larger principles. Hopefully this will help you a bit.

我只会回答你提出的一些棘手的问题,并解决更大的原则。希望这会对你有所帮助。

1) No, you don't have to use strongly typed views. I never do. Personally, I prefer the MVCContrib strongly typed viewdata.get (viewdata.get("user");).

1)不,您不必使用强类型视图。我从来没有做。就个人而言,我更喜欢MVCContrib强类型的viewdata.get(viewdata.get(“user”);)。

2) Yes, the xVal (and other similar libraries) help you from doing the same stuff on the server side and client side. However... you have to set up the validation rules in the first place.

2)是的,xVal(以及其他类似的库)可以帮助您在服务器端和客户端执行相同的操作。但是......您必须首先设置验证规则。

For things like [Required] or [StringLength] this has already been done for you. But there'll always be the possibility that you need more complicated validation. And in this case, you need to define the routines separately (different languages, different elements, varying access to data (databases on the server)).

对于[必需]或[StringLength]这样的事情已经为你完成了。但是总有可能需要更复杂的验证。在这种情况下,您需要单独定义例程(不同的语言,不同的元素,不同的数据访问权限(服务器上的数据库))。

The IsPasswordSafe attribute is a good example. The constructor just sets the javascript regular expression (in this case ".{8,20}" returns true if the value is between 8 and 20 characters) -- a VERY simple check. My assumption is that after that string is set in the constructor, the functions responsible for creating the clientside javascript will read it. On the serverside (the PropertyValid() method), you can do more intensive things -- check a dictionary, check that user's previous passwords, etc -- that could NOT be done locally.

IsPasswordSafe属性就是一个很好的例子。构造函数只是设置javascript正则表达式(在这种情况下“。{8,20}”如果值在8到20个字符之间则返回true) - 非常简单的检查。我的假设是在构造函数中设置了该字符串后,负责创建clientside javascript的函数将读取它。在服务器端(PropertyValid()方法),你可以做更多密集的事情 - 检查字典,检查用户以前的密码等 - 这是不能在本地完成的。

I suggest you re-read the posting with the above in mind, and play around a bit, and hopefully everything will become clearer.

我建议你重新阅读上面记载的内容,然后玩一下,希望一切都会变得更加清晰。

On a personal note, I've just started using xVal and I'm pretty happy with it. I haven't gotten into custom validation (maybe you should start playing with the included DataAnnotations stuff). But one of the points of xval is that you don't need to know anything about jquery.validate. Just know that the server side and client side don't always have to be -- and sometimes can't be -- in sync, validation-wise.

就个人而言,我刚刚开始使用xVal,我对它非常满意。我没有进入自定义验证(也许你应该开始使用附带的DataAnnotations东西)。但是xval的一个要点是你不需要了解jquery.validate的任何信息。只要知道服务器端和客户端并不总是必须 - 有时不能 - 同步,验证方面。

James

#2


In my understanding, the article describes two different ways of client validation:

根据我的理解,本文描述了两种不同的客户端验证方法:

  1. Remote client validation, where client sends an AJAX request to server, asking server to validate. This is different from pure server side validation since it does not require page reload. The article shows how to perform remote client validation without any additional code.

    远程客户端验证,客户端向服务器发送AJAX请求,要求服务器验证。这与纯服务器端验证不同,因为它不需要页面重新加载。本文介绍如何在没有任何其他代码的情况下执行远程客户端验证

  2. Pure client validation, this is what ClientSideRegEx is used for -- in this case there is no server request at all, but more code is required.

    纯客户端验证,这就是ClientSideRegEx用于的 - 在这种情况下根本没有服务器请求,但需要更多代码。

As for RemoteValidation.Models.User -- you can pass any other class, as long as it uses annotation attributes on its properties.

对于RemoteValidation.Models.User - 您可以传递任何其他类,只要它在其属性上使用注释属性即可。