The validator is used to validate input. It can be used to validate anything since both name and value are passed to it.
It also have a static variable called Language which can be used to assign languages to it.

I'll use the term LangCat in the text below. It stands for Language Category. A LangCat contains multiple languages for a specific part of the system.

Usage

At startup:
    Validator.Language = myLangMgr.GetCategory("Validator");


And to validate (the Validator class have been moved to the .Net framework addons project:
  Validator validator = new Validator(myLangMgr.GetCategory("user"));
  string userName = validator.GetLettersOrDigits("username", Request.Form["username"]);
  if (validator.ContainsErrors)
    return "First error: " + validator.Errors[0];


Using a form is quite common in a web application =) Therefore we have a convenience class called FormValidator.

  FormValidator validator = new FormValidator(Request.Form, myLangMgr.GetCategory("user"));
  string userName = validator.GetLettersOrDigits("username");
  if (validator.ContainsErrors)
    return "First error: " + validator.Errors[0];

Model language

As you see in the above example we still supply a LangCat to the constructor, despite that we've specified one with the static variable.
The LangCat that we pass to the contructor is used to translate the field names.

"Username" in the above example becomes "Användarnamn" in Swedish thanks to the specified LangCat.

Language translations.

In the validator class you some static constants called FieldXXXXX (as in FieldRequired). The contain the key name of the phrases to translate. Their documentation also specifies what the phrases should say.

Here is the name and their contents
  • Required '{0}' is required.
  • Number {0}' is not a number.
  • LetterOrDigit '{0}' can only contain letter or digits.
  • Password '{0}' can only contain letter or digits or one of the following chars: {1}
  • Email '{0}' is not a valid email address.
  • ValueTrue true
  • ValueFalse false
  • Boolean '{0}' must be {1} or {2}.

Last edited Aug 25, 2008 at 5:05 PM by jgauffin, version 3

Comments

No comments yet.