views:

190

answers:

2

Hi

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.

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?

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”

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?

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 != “[email protected]”;
}

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?

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

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?

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.

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.

+1  A: 

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");).

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.

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)).

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.

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.

James

James S
Thanks.Ya I re-read the article again and my questions have been answered by the guy. Now that I gone through it line by line with the debugger I understand it more.
chobo2
The only thing I am still not clear on but I have not even looked on it is. If I want to do some other regex. Should I use the same property or can I make a new property.Of course I also could just used the predefined label for regex and skip the property all together.
chobo2
A: 

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.

  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.

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

Andrey Shchekin