2

I would love to use the @NotNull annotation (or @Required or anything) for my own methods. While this works quite well in Controller and Model classes I cant get it to work in my own. This probably is more of a net.sf.oval question then play framework. But it might be connected, I don't know.

I have a class like:

@net.sf.oval.guard.Guarded
public class SimulatorWrapper {

    public SimulatorWrapper setRedCode(@play.data.validation.Required @net.sf.oval.constraint.NotNull final String redCode) {
        // just gessing here:
        if(Validation.hasErrors()) throw new RuntimeException("invalid argument");
        if(redCode == null) throw new RuntimeException("null");
        // do stuff
        return this;
    }
}

When I call this method with a null parameter the if throws my exception, but @NotNull and @Required seem to do nothing at all. What am I doing wrong? The play framework project came with oval 1.5, I downloaded 1.8 and added it to the classpath in eclipse just in case the old one had problems.

I'm starting the server with "play test my-server" and then I navigate to my website (not a test yet, just simple site) with my browser.

Thanks, Alex

P.S. I know the "null is evil" discussion, but I dont have access to the rest of the code so I cant change that.

Alex
  • 474
  • 3
  • 10

2 Answers2

1

In case anyone still needs this.

You can do validation with Play annotations in all classes exactly the same way as in controllers. Just use validate plugin.

Marek Piechut
  • 557
  • 4
  • 9
1

The validation class is invoked to check the validation annotations by the Play framework only when a controller action is called.

Since you're not in a controller, the Validation on annotation won't be executed and the Required annotion won't be in Validation.hasErrors()

Instead of using annotation, you could use methods like: Validation.required(redCode); //It'll check for null And after that, call Validation.hasErrors() and it should work.

However, I don't think you should do this because the errors from Validation.hasError() should come from Validation on the controller action invocation and it can cause you side effects.

If you want to do something like your example, you should not rely on the play Validation class.

Are you sure you're using validation at the right places ?

Mike
  • 2,354
  • 3
  • 23
  • 37
  • hi, first of all thanks for the reply. Of course this is the wrong place to use validations, but I'm dealing with really stupid code that I cant change so I want to add @NotNull to pretty much every single parameter of my functions. (In the future maybe even more restrictions). – Alex Dec 21 '11 at 07:12
  • hups enter sent it away. so anyway, I was thinking of using net.sf.oval for that - isnt that the purpose of that library? Because as you said plays validation is only ment for controllers and models. At the moment I have lots and lots of "if(... == null) throw ..." – Alex Dec 21 '11 at 07:14
  • I now got to void test(@NotNull String a) { new Validator().assertValid(a); } which fails if I pass null. But thats pretty much the same as using if null then throw. As far as I understand I would need to use an aspectj compiler for automatic vaidation, but I cant find anything on how to do that with play framework. – Alex Dec 21 '11 at 10:32
  • 1
    I think play wanted to stay away from thing like aspectJ because it's too complicated. Maybe you could also use an Enhancer to add stuff to you classes but there's no official documentation about that so it will be more difficult. There's a couple of enhancer in the framework that you can use as an example like : LocalvariablesNamesEnhancer – Mike Dec 21 '11 at 14:47
  • 1
    ah thanks, that pointed me to Javassist and I finally decided thats its sooooo not worth the effort! :D maybe some time in the future with aspectj in case it ever gets more popular (it sounds really nice) – Alex Dec 21 '11 at 18:48