#932 new
Peter Hilton

Change play.data.validation.Validation.Validator field from Annotation to check

Reported by Peter Hilton | June 23rd, 2011 @ 03:04 PM

Issue

It is currently not possible to programmatically add validators for a class at runtime, because play.data.validation.Validation.Validator has a field of type java.lang.annotation.Annotation, and you cannot instantiate an annotation in Java code. In other words, there is no way to add validators that will be returned by the play.data.validation.Validation.getValidators methods; these can only return validators specified by model property annotations.

Use case

There are two common use cases for dynamically adding a validator at run time. The first is that you want to validate a value that does not correspond to a model property, such as a separate time field that will be combined with a date (without time of day) field. The second is that validations can be dynamically enabled or disabled, such as an application where ‘required’ is a user preference instead of being hard-coded on the model.

On the server side, Play handles dynamic validation by letting you call play.data.validation.Validation methods directly in a controller action, e.g. validate.required(name);. However, this does not help if you want to implement client-side validation based on Play validation definitions.

One way to implement client side validation is to use the jQuery Validator plug-in -http://docs.jquery.com/Plugins/validation which you configure with a ‘rules’ object for form fields:

var rules = {
   rules: {
     name: "required”,
     email: {
       required: true,
       email: true
     }
   }

If you only use model annotations for validation, then you can generate the rules definition in a Play template:

var rules = {
%{
    controllers.Validation.getValidators(_class, _key).each() {
        out.print "'" + it.key + "': { "
        for(validator in it.value) {
            switch (validator.annotation.annotationType().simpleName) {
                case "Required":
                    out.print "required: true, "
                    break;
                case "MinSize":
                    out.print "minlength: " + validator.annotation.value() + ", "
                    break;
                case "Range":
                    out.print "range: [" + validator.annotation.min() + "," + validator.annotation.max() + "], "
                    break;
                case "Email":
                    out.print "email: true, "
                    break;
                case "Equals":
                    out.print "equalTo: 'input[name=" + validator.params.equalsTo + "]', "
                    break;
                case "Url":
                    value = "url: true";
                    break;
            }
        }
        out.println "},"
    }
}%
};

The problem is that if you have additional dynamic validations, you currently have to add them to this rules object on the client side, instead of having them also picked up by play.data.validation.Validation.getValidators.

Possible solution

Since the problem is not being able to construct a play.data.validation.Validation.Validator instance because it requires an annotation instance, a solution would be to change the implementation of play.data.validation.Validation.Validator to replace its annotation field with a net.sf.oval.configuration.annotation.AbstractAnnotationCheck field, and look-up the check class for each annotation found.

The impact is probably limited to code inside the play.data.validation.Validation class.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2>Play framework</h2>

Play makes it easier to build Web applications with Java. It is a clean alternative to bloated Enterprise Java stacks. It focuses on developer productivity and targets RESTful architectures. Learn more on the <a href="http://www.playframework.org">http://www.playframework.org</a> website.<br><br>

<h2>Source code is hosted on github</h2>Check out our repository at <a href="http://github.com/playframework/play">http://github.com/playframework/play</a><br><br>

<h2>Contributing, creating a patch</h2> Please read the <a href="http://play.lighthouseapp.com/projects/57987/contributor-guide">contributor guide</a><br><br>

<h2>Reporting Security Vulnerabilities</h2> Since all bug reports are public, please report any security vulnerability directly to <em>guillaume dot bort at gmail dot com</em>.<br><br>

<h2>Creating a bug report</h2> Bug reports are incredibly helpful, so take time to report bugs and request features in our ticket tracker. We’re always grateful for patches to Play’s code. Indeed, bug reports with attached patches will get fixed far quickly than those without any.<br><br>

Please include as much relevant information as possible including the exact framework version you're using and a code snippet that reproduces the problem.<br><br>

Don't have too much expectations. Unless the bug is really a serious "everything is broken" thing, you're creating a ticket to start a discussion. Having a patch (or a branch on Github we can pull from) is better, but then again we'll only pull high quality branches that make sense to be in the core of Play.

Tags

Pages