This is a shameless plugin, but can I suggest a framework that I developed? I built it based on annotations (a la Hibernate Validator). It supports user restrictions, and I feel that it is quite powerful. Here is also a Stackoverflow question, where I asked for an overview of the structure.
- Presentation Using special validation constraints, you can bind validation to the onChange element. In addition, since Regula supports custom validators, you can configure your own validator for the field value (so change
5 to 5.00 ). - Side Effects : Regula supports side effects with custom limiters.
- Groups : Regula supports validation groups. You can set up target groups for verification. By combining a custom validator and a group, you can control the behavior of the validator so that it only checks when all the elements of this group are full (you will have to perform this verification through regular Javascript).
- Server side validation . With user restrictions, you can make an AJAX call for server-side validation. Since the structure is currently structured, this must necessarily be a blocking ajax call. I plan to add an asynchronous function in the future.
- Multiple forms per page : Regula is not limited to checking one form per page. It can handle multiple forms (not sure if I understood your requirement correctly - so I might not answer this part correctly).
- Custom error display: Regula does nothing with the page's user interface with respect to validation. When you check, you get a set of constraint violations containing error messages, etc. It is up to you how to display them.
- Snappiness: I have not performed any tests, so I can not comment on the performance of my infrastructure in this regard.
- Send buttons: This is what I have yet to solve (asynchronous or synchronous).
Here are some examples:
The following is a standard check with built-in restrictions:
<input id = "myInput" name = "myInput" type = "text" class = "regula-validation" data-constraints = '@NotEmpty @IsNumeric @Between(min=1, max=5)' /> jQuery(document).ready(function() {
As you can see, you only work with violation of restrictions, and therefore the method of displaying the error message is completely up to you.
Here is an example of a user constraint:
regula.custom({ name: "MustBe42", defaultMessage: "The answer must be equal to 42", validator: function() { return this.value == 42; } });
And its use:
<input id = "theAnswerToLifeTheUniverseAndEverything" name = "theAnswerToLifeTheUniverseAndEverything" value = "" class = "regula-validation" data-constraints = "@MustBe42" />
Since the validator is a Javascript function, you can force it to do something (so that concerns your question about side effects).
Here is an example of another constraint that takes parameters:
regula.custom({ name: "DivisibleBy", defaultMessage: "{label} must be divisible by {divisor}", params: ["divisor"], validator: function(params) { var divisor = params["divisor"]; return (this.value % divisor) == 0; } });
And use:
<input id = "number" name = "number" value = "" class = "regula-validation" data-constraints = "@DivisibleBy(divisor=3, label='The Number')" />
Here is an example of using validation groups:
<input id = "score" name = "score" type = "text" class = "regula-validation" data-constraints = '@IsNumeric(label="Score", message="{label} needs to be a number!" groups=[FirstGroup, SecondGroup, ThirdGroup]' /> <input id = "age" name = "age" type = "text" class = "regula-validation" data-constraints = '@IsNumeric(label="Age", message="{label} needs to be a number!" groups=[SecondGroup]' /> <input id = "name" name = "name" type = "text" class = "regula-validation" data-constraints = '@NotEmpty(label="Name", message="{label} cannot be empty!" groups=[FirstGroup]' />
And a fragment that only FirstGroup checks (so only score and name are checked):
var constraintViolations = regula.validate({groups: [regula.Group.FirstGroup]}); var messages = ""; for(var index in constraintViolations) { var constraintViolation = constraintViolations[index]; messages += constraintViolation.message + "\n"; } if(messages != "") { alert(messages); }
If you plan to try this, I recommend downloading version 1.1.1 . Current documentation is consistent with this version. In 1.2.1, I added support for complex constraints, but I did not update my documentation to reflect this.
I understand if this does not apply to all your problems, or if this is not what you are looking for. I thought I was just putting out. Also, if you do, I will definitely update the documentation to reflect version 1.2.1 . I was busy with school and work, so I did not have time for this.
UPDATE # 1
Sohnee mentioned client-side validation. I'm actually working on integration between Regula and Spring 3. I hope I can release it sometime soon (again, at work and at school). Integration works by translating Hibernate validation restrictions to validation restrictions. Thus, you only need to write the verification code once (basically). However, for user restrictions, you still have to write code on the Javascript side (custom validator). But as soon as you comment on the server side code with the limitations of the Hibernate check, you do not need to do anything on the client side; these restrictions are automatically applied to the formation of elements on the client side.
Matthew Abbott was also able to integrate Regula with ASP.NET MVC .
UPDATE # 2
I have a demo webapp (mavenized) on github that demonstrates the integration between Regula and Spring 3.0.x Web MVC using the Hibernate Validator. It really is not documented or anything else, it is more proof of concept. I plan to add some documentation to the github page about integration and how it works.
UPDATE # 3
I updated the documentation on the wiki and now it corresponds to the latest version 1.2.2 (I made a small correction, so it is 1.2.2 ).