Spring mvc form validation using hibernate validator - spring

How to display a custom message or remove input for integer field in jsp form when user enters white-space in input-field in spring mvc, validated using hibernate validator?
It currently shows:
Failed to convert property value of type java.lang.String to required
type int for property freePasses; nested exception is
java.lang.NumberFormatException: For input string: ""

Use wrapper class to declare integer variables so that wrapper class will convert whitespace to null.
or
refer below link for common solution
Hibernate validation annotation - validate that at least one field is not null

Related

Spring MVC cannot convert from java.lang.String to java.util.ArrayList

I'm selecting values in check boxes and passing them over to the form backing bean. These values are User objects that are stored in an ArrayList<User> in a form backing bean.
However, when I submit the form I get the above error and then the log says something like "there is no converter that can handle this conversion".
First of all, why is it even storing the Userobjects as an array of Strings? String[]
Then it is trying to convert it to ArrayListbut cannot find a converter?
I don't understand.

Thymeleaf th:value

Using SpringMVC + Thymeleaf, how does one bind an integer based model attribute, to an input field in a form using th:value or th:field, without having a value of '0' show in the field itself.
The issue is not with Thymeleaf, it is on the EL implementation of Tomcat. It doesn't respect the difference between the nullable Integer and primitive int. It is always coercing null values to 0. You can turn off this behavior using this VM argument :
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
or programatically by
System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
If you choose the programatic way, make sure that you invoke it during initialization of ServletContext (ServletContextListener#contextInitialized)
Just remove th:field attribute and write id and name attributes with corresponding logical name.

Missing Converter when using Spring LdapTemplate with Grails Validateable annotation

I'm using the Spring LDAP (docs) library in a Grails application. I have a class annotated with the #Entry annotation, so it is mapped to an LDAP server. This all works quite beautifully.
However, when I add the Grails #Validateable annotation (to enable validating the LDAP class similarly to Grails domain classes) and attempt to retrieve data from LDAP (i.e. a findAll operation on the LdapUserRepo, or similar), I get the following exception:
Message: Missing converter from class java.lang.String to interface org.springframework.validation.Errors, this is needed for field errors on Entry class com.ldap.portal.LdapUser
Basically, it seems like the AST transformation performed by the #Validateable annotation is producing extra fields (namely the errors field) on the LdapUser object. It appears that Spring LDAP, in processing the #Entry logic, assumes a default mapping for the fields property (probably interpreting it as a string field on the LDAP object). When it gets nothing from the LDAP server, it attempts to set the field of type ValidationErrors to a value of type String -- an empty string.
I did some looking in github and found this code that seems relevant and may support my theory.
My question is: is this behavior expected for annotations, and how can one prevent fields added by one annotation from being inappropriately processed by another annotation?
At present the best workaround I've come up with for my specific issue is to add an errors field to my LdapUser object and mark it as transient (so that LDAP ignores it):
#Transient
ValidationErrors errors

What options are there to do output validation of Jersey resources?

What are my options to do output validation of entities in Jersey? Simple use case would be to validate that a returned User instance doesn't have a certain field set to non-null.
Not sure of the context, it this at runtime, or during unit tests?
Bean Validation is one very popular validation framework.
You can readily use it to validate your input (see #NotNull, #Valid and other bean validation annotations with Jersey) ref : https://jersey.java.net/documentation/latest/bean-validation.html
If you want to validate your output, you can use the bean validation framework also, but you will need to use it manually and handle errors.
just check out one of many tutorials on bean validation : http://java.dzone.com/articles/bean-validation-made-simple

Using Spring's MessageSource for setting FieldError default messages

After my form backing object is validated I have a BindingResult, which contains a list of FieldError. Each FieldError has a defaultMessage. How is that message set and why doesn't it use my Spring MessageSource? I would like that default message to be derived from my Spring's MessageSource.
EDIT:
I see that the error codes are being set correctly in the FieldError object. It's just the default message in that object is not coming from my MessageSource. For instance, when I enter a string for a field that is an int I want it to get my message from messages.properties:
typeMismatch=Invalid type was entered.
The only way I can get that message is if I take my FieldError object and pass it into the MessageSource manually like so:
messageSource.getMessage(fieldError, null); // This gets my message from messages.properties.
If you're using a Validator, you can specify the keys for the messages in the MessageSource in the Validator implementing class, usually using ValidationUtils methods. Section 6.2 of the Spring documentation has a good example.
Spring will also try to resolve error codes by convention if you're using something other than a Validator like JSR-303 Bean Validation.
Let's say you had a form backing object called 'Address' with an int field called 'zipcode.' If the user entered a string for the zipcode field, by default Spring will use the DefaultMessageCodesResolver and look in the MessageSource for a key called 'typeMismatch.address.zipcode.' If it doesn't find that key, it will try 'typeMismatch.zipcode,' then 'typeMismatch.int,' then 'typeMismatch.'
Or, you can implement your own MessageCodesResolver.

Resources