andrew
andrew

Reputation:

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.

Upvotes: 1

Views: 356

Answers (5)

diadiora
diadiora

Reputation: 599

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.

Upvotes: 1

DM.
DM.

Reputation: 1847

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.

Upvotes: 1

griegs
griegs

Reputation: 22760

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.

Upvotes: 4

Jay Zeng
Jay Zeng

Reputation: 1421

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

Upvotes: 0

Related Questions