views:

126

answers:

5
+1  Q: 

Custom Validation

How are people doing their validations on data?

Basically I have a requirement that to apply for insurance you need to be over 14 years of age.

Now, on the application form you may need to not only enter your age but any nominated drivers date of births as well.

I went through the Nerd Dinner example and it works but I was wondering whether anyone is doing other types of validations.

A: 

Nerd dinner was developed as an example for the "Professional ASP.NET MVC 1.0", its first chapter is available for free that basically walks through the entire application and covers validation and custom validations. You may download it at:

http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf

Jay Zeng
I've already looked at the Nerd Dinner approach. I was looking for other, perhaps auomated, ways.
+2  A: 

You can use DataAnnotations to attach validation to objects directly.

Validating with Data Annotation Validators

You can get fancy by creating custom data annotations which will then allow you to create validation on fields of a certain type.

So for your age requirement;

So;

public class IsApplicantOldEnoughAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value == null) return false;
        DateTime enteredDate = (DateTime)value;

        if ((DateTime.Today.Year - enteredDate.Year) >= 14)
            return true;
        else
            return false;
    }
}

Your model could then decorate the field;

[IsApplicantOldEnough(ErrorMessage="Applicant must be over 14 years of age")]
[Required]
public DateTime DateOfBirth { get; set; }

Then in your view;

<p>
  <label for="UnitPrice">DOB:</label>
  <%= Html.TextBox("DateOfBirth")%>
  <%= Html.ValidationMessage("DateOfBirth", "*")%>
</p>

Then your Controller could look like this;

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Apply([Bind(Exclude = "Id")]Applicant newApplicant)
{
  if (!ModelState.IsValid)
    return View();

  return RedirectToAction("Success");
}

This is a bit more work but you no longer need to call a method yourself everytime you wanted to validate some data.

It also means that all applications that use this model will apply the same business rule to your ages thus providing consistency across the organisation.

I actually happened to have the above handy to some respect. I use it a lot in my objects. Do remember to wrap this in a Try / Catch.

griegs
Thanks griegs this looks like the type of thing I might be looking for.
+1  A: 

I'm a huge fan of xVal. It's super simple to use, you can create custom validation rules very easily if need be, and it's integration with jQuery Validation is wicked. Give it a look.

DM
+1 for xVal. I'm using it with great success. I've written additional ActionFilter that does automatic data validation for me.
Robert Koritnik
Sounds cool, I'd love to see what you did in that ActionFilter.
DM
+1  A: 

From practice the best is to use FluentValidation. The only disadvantage is how to use it on client side, but if your application need more elaborate client side messages and styles than are used in xVal, than this is the way to go, in other case use xVal.

diadiora