Validation on an external model object using play framework 1.2.5 - validation

I have a model object which is defined outside of my play application, it is an auto-generated entity produced from wsdl. This means that I can't reliably add validation annotations to its fields. I thought about extending the class and in fact that's what I've done, and I wonder if its possible to add validation tags to the extended version? An immediate problem that I see with that however is that it also has children who's members would also need validation tags added, and if i were to extend them, they would not be used by play's binder. I guess what I need is some kind of custom validator, but from what I can see they are aimed at validation single properties, not entire object models.
Any pointers or suggestions would be great
Cheers!
NFV

You could write custom validator for your class and use:
public static void myController(#CheckWith(MyValidator.class) myParameter)
in controller to test if objects are valid. Then just manually validate them inside MyValidator (it has to extend play.data.validation.Check).
Check play documentation for more info about custom validators.

Related

Web API - how to have custom display name for model validation properties?

If I have an ApiController which has a post method which consumes a model which has data annotations for validation, how do I customize the name displayed for the validation errors? I don't really want to override the entire error message, but I do need the property in the error to be something other than the name of the property on the model type.
I've tried things like DisplayNameAttribute and DisplayAttribute, but those don't seem to apply for this.
The best solution I found for this was based on the following:
https://gist.github.com/benfoster/4016852
You have to create your own validator provider (as shown in link), and your own validator which will set up a ValidationContext with the proper displayName such that
validationContext.DisplayName = displayNameMappingFunction(metadata.GetDisplayName());
You then need to register the validator provider globally by using GlobalConfiguration.Services, or you need to create an IControllerConfigurationAttribute which will configure it just for one controller.
You do config.Services.Replace(typeof(ModelValidatorProvider), new CustomDisplayNameProvider()

JSF 2 and manual validation with JSR 303 - how to track fields?

I want to use JSR 303 (Hibernate Validator) in my JSF 2 project. However, I have complicated forms and field-level validation is not sufficient. I need to use many #ScriptAssert annotations at class-level of my model and its child beans.
So I want to validate form models manually (inside bean action method for example). But I could not understand how I can preserve which validation message should be shown at which field (as it works automatically when field-level validation is on and processed by JSF).
Also I'll need to specify for some of class-level annotations that their messages are to be shown at specific fields. I do not see a straight way to manage it...
Could you please provide a link to explanations of these questions (or tell me that I am doing something wrong?). I think I fail in googling it because internet is bloating with keywords JSF and validation, sorry.
The most idiomatic way to do that is to create custom Bean Validation validator for your class.
You need to create validation annotiation that you would put at the class level (not field level) and associated with that class validator class.
See for instance:
http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-usingvalidator.html (Example "Class level constraint")
How can I validate two or more fields in combination?
Validation inside managed bean is also possible, you can throw in your action methods proper validation exception, but usually it is cumbersome, hard to reuse and mixes business logic code with validation.

parameter validation with net.sf.oval (in play framework)

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.
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 ?
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.

Spring - data validation

I'm just wondering about data validation in Spring. Spring is offering two (maybe more) validation options. First option is to use Validator interface and create whole validation on my own. Second option is to use annotation validation (JSR 303).
And now I'm really confused :) which validation I should chose. What I need is to check if recieved Data Object is correct (by correct i mean all required fields are filled) and this can be done by JSR 303 validation or by my own validator with "Validator instance". But I also need to check if is this Data Object valid against some database constraints (validator is required to check some data in database, eq. is user with this ID registered or not ...) and this can be done only by my own validator.
I don't know which way should be the best. Combine both or create my own validator ?
I will be thankful for any advice ...
Update (relocated from comments)
Ok, I followed Ryan's example and I think I was successful. I created my own implementation of spring Validator and in this Validator I #Autowire-d javax JSR 303 instance. But there was problem with that injection. I had in my configuration and this piece of code caused some exceptions, because spring did not know which Validator I want to inject. So I removed that code.
At the end I also removed the spring Validator implementation, because I dont know where I can get Errors property, which is required as second parameter in "validate" method :). I'm triggering that validation manually from service layer and I really don't know, where I can obtain that Error object.
BTW Well, I found another solution how to implement that validation. I'm thinking about to extend my validator class by LocalValidatorFactoryBean. LocalValidatorFactoryBean class implementing both Validator interfaces (Spring Validator + JSR 303). But i'm not sure if is this good approach. This approach also require Error object, which I don't know where to find/get.
Update
The Error object is coming from "BindException".
FooObjectVO myObject = new FooObjectVO();
BindException errors = new BindException("fooObject", myObject);
They're not really separate options--more like two available implementations of validation. Since Spring's JSR 303 support also implements its own Validator interface, you don't really have to pick one or the other. You can mix and match the implementations in whatever way makes it easiest to get the job done.
In cases such as this I prefer to combine both. I like to use JSR 303 validation for whatever I can. I supplement it with my own validators.

struts2 validation invoking xml

i have a login-validation.xml which define some basic field validation rules.
however that's not enough for me.
i need to do some more database lookup and i consider this as part of my validation logic.
how can i do both xml validation and my database lookup in one go?
i suppose i will write something like
public void validate() {
1) struts2-validation.xml validation();
2) myDatabaseLookup() and addFieldError() or addActionError();
}
my problem is, what is the api i can use for (1)?
or, how can i look at the code of this xml validation filter class? in fact i would also make the definitions in validation.xml available to javascript usage... i guess i would need to do some translation from xml to javascript logic, but first of all, how can i access the validation.xml api in java code?
Your best choice is to create a validator... Take a look here for some information -
Custom Validator
There are a few things to keep in mind... I don't know if the ObjectFactory will instantiate and inject your validator, so you might not have all the features of dependency injection. If your custom validator isn't injected, file a bug, I'll take a look at it.
After you create your validator and register it in your app, you can add it to the validation.xml file.
(side note, I know that I am pointing to the XWork docs, but Struts2 uses XWork internally for most of it's validation capabilities)

Resources