limcheekin / jquery-validation-ui

JQuery Validation UI Plugin - Client Side Validation without writing JavaScript

Home Page:http://limcheekin.github.io/jquery-validation-ui/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

validate looking for a String or Long value for id

mulcahy opened this issue · comments

JQueryRemoteValidatorController seems to be expecting a String or Long for the id parameter type. When using MongoDB as the domain object's persistence store it creates an issue as the object type is an org.bson.types.ObjectId. The check in the controller for a String fails and it then tries to convert the id value to a Long, throwing an exception.

Please find the exception below:
2013-08-27 09:50:10,926 [http-bio-8080-exec-9] ERROR StackTrace - Full Stack Trace:
java.lang.NumberFormatException: For input string: "520a5902e4b0ee2a6f3dc4f8"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at java.lang.Long.valueOf(Long.java:540)
at org.grails.jquery.validation.ui.JQueryRemoteValidatorController$_closure1.doCall(JQueryRemoteValidatorController.groovy:39)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

Thanks for creating this issue.

You are right! JQueryRemoteValidatorController is expecting a String or Long for the id parameter type. May I know do you want the id to convert to String for your scenario?

I think a String will do. Perhaps if the Elvis order was to check for type Long and then default to String otherwise, it might fix this issue.

I (quickly) tested this approach where I switched the default from Long to String.
// previous
// def id = GrailsClassUtils.getPropertyType(validatableClass, 'id') == String?params.id:params.id.toLong()
// test
def id = GrailsClassUtils.getPropertyType(validatableClass, 'id') == Long?params.id.toLong():params.id

This seems to work okay for me (it doesn't throw an class cast exception). However, I'm not familiar enough with the code to know if this will adversely affect some other aspect (such as performance).

Since I was looking at the code, I have a follow-on question about the next line:
validatableInstance = validatableClass.get(id)?:validatableClass.newInstance()
Does this create a DB query and retrieve the instance (again)? I would have thought the new incoming params should be associated with the validatableInstance and not the old persisted version. I'm very open to correction on this :-)

Thank you for the prompt response!

Thanks for proposed solution. I incorporated your suggestion to 1.4.7 release that just published.

The persisted version of validatableInstance is needed for unique constraint validation for non-id property.

Hope this clarify.

Thank you for that.

ezoic increase your site revenue