limcheekin / jquery-validation-ui

JQuery Validation UI Plugin - Client Side Validation without writing JavaScript
http://limcheekin.github.io/jquery-validation-ui/
Apache License 2.0
20 stars 18 forks source link

Fix for issue #15 broke the display of field values in validation error messages. #24

Closed jonroler closed 11 years ago

jonroler commented 11 years ago

The fix for issue #15 added the encodeAsJavaScript() call at the end of JqueryValidationService.getMessage() in order to allow single quotes inside of message strings. This method escapes single quotes in the message which is exactly what is needed to allow arbitrary strings to be placed inside of a javascript string. However, this type of escaping can't be performed on actual javascript code, which is what is happening as a result of issue #15:

validation-javascript

We are passing actual javascript (' + $('#name').val() + ') to get the value of the field included in the message string. However, when the single quotes are escaped, they just appear inline in the code.

The best fix I can think for this is:

  1. First pass a placeholder string (like "JAVASCRIPT_TO_RETRIEVE_VALUE_GOES_HERE") as an argument to messageSource.getMessage()
  2. Call encodeAsJavascript() on the result (the placeholder will still be present here).
  3. Finally, replace the placeholder with the actual javascript.

This solution allows the message to be escaped for quotes as needed, and the javascript can then be added in the correct place without the escaping. The main downside to this approach is some arbitrary placeholder text has to be chosen that hopefully won't appear elsewhere in the message. I don't see any way around this, however, so we'll just have to pick something very likely to not appear in any actual message.

jonroler commented 11 years ago

Proposed fix is included in https://github.com/limcheekin/jquery-validation-ui/pull/25. With the fix, values now display correctly in javascript messages (instead of displaying the javascript code itself). This bug is actually pretty bad since many of the default grails error messages include the value, so people (like me) who just decide to try out this plugin with a new project will immediately encounter this bug.

limcheekin commented 11 years ago

Good catch! Thanks for report the issue with fix provided. This fix was just released in 1.4.4.