Issue Details (XML | Word | Printable)

Key: SHALE-325
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Wendy Smoak
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Shale

Add a custom validation example to the Use Cases example app

Created: 06/Nov/06 10:23 PM   Updated: 23/Jan/07 04:40 PM
Return to search
Component/s: Validator
Affects Version/s: None
Fix Version/s: 1.0.4


 Description  « Hide
Add an example of using a custom validator to the Commons Validator Integration section of the use-cases example app.

 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #471907 Mon Nov 06 22:39:18 UTC 2006 wsmoak Adding an example of a custom validation rule to the use cases app. 'Priority' must be an even integer.
Unfortunately, it doesn't quite work. Entering an odd number causes a NPE when it tries to format the message.
SHALE-325
Files Changed
MODIFY /shale/framework/trunk/shale-apps/shale-usecases/src/main/resources/org/apache/shale/usecases/view/Bundle.properties
MODIFY /shale/framework/trunk/shale-apps/shale-usecases/src/main/webapp/WEB-INF/web.xml
ADD /shale/framework/trunk/shale-apps/shale-usecases/src/main/java/org/apache/shale/usecases/validator/CustomValidationUtil.java
MODIFY /shale/framework/trunk/shale-apps/shale-usecases/src/main/java/org/apache/shale/usecases/validator/Procedure.java
MODIFY /shale/framework/trunk/shale-apps/shale-usecases/src/main/webapp/validator/test.jsp
ADD /shale/framework/trunk/shale-apps/shale-usecases/src/main/webapp/WEB-INF/custom-rules.xml

Wendy Smoak added a comment - 06/Nov/06 10:43 PM
See: http://svn.apache.org/viewvc?view=rev&rev=471907

This is incomplete. I added a third section on the 'Commons Validator Integration' page. If you enter an odd number for 'Priority', you get:

java.lang.NullPointerException
java.text.MessageFormat.applyPattern(MessageFormat.java:414)
java.text.MessageFormat.<init>(MessageFormat.java:368)
org.apache.shale.validator.CommonsValidator.getErrorMessage(CommonsValidator.java:965)
org.apache.shale.validator.CommonsValidator.validate(CommonsValidator.java:849)
javax.faces.component._ComponentUtils.callValidators(_ComponentUtils.java:157)
javax.faces.component.UIInput.validateValue(UIInput.java:312)
javax.faces.component.UIInput.validate(UIInput.java:353)
javax.faces.component.UIInput.processValidators(UIInput.java:183)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:624)
javax.faces.component.UIForm.processValidators(UIForm.java:70)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:624)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:624)
javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:146)
org.apache.myfaces.lifecycle.LifecycleImpl.processValidations(LifecycleImpl.java:262)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:137)
org.apache.shale.application.faces.ShaleApplicationFilter.doFilter(ShaleApplicationFilter.java:267)


Gary VanMatre added a comment - 06/Nov/06 11:24 PM
I think your problem is that the CommonsValidator can't find the message.
There are a couple of places that you can register a custom message.
You can load the resource bundle within the page and use the msg attribute.


<val:commonsValidator
                   type="evenInteger"
                   msg="#{messages['validate.test.not.even']}"
                   server="true"
                   client="false"/>


-- or ---

Register the resource bundle in the faces-config.xml

<application>
    <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>en</supported-locale>
      <supported-locale>fr</supported-locale>
      <supported-locale>de</supported-locale>
      <supported-locale>es</supported-locale>
    </locale-config>

    <message-bundle>org.apache.shale.usecases.view.Bundle</message-bundle>
</application>


Wendy Smoak added a comment - 07/Nov/06 02:29 AM

The error message key is specified in the rule itself, in custom-rules.xml.

   <global>
       <validator name="evenInteger"
             classname="org.apache.shale.usecases.validator.CustomValidationUtil"
             method="isEven"
             methodParams="int"
             msg="validate.test.not.even">
       </validator>
   </global>

There is no <message-bundle> tag in faces-config, and adding it didn't help.

The attribute for val:commonsValidator is 'message' (not msg). Adding that fixes the NPE, but see above-- the message is specified in the rule, I shouldn't have to say it again.

What do I need to do so that CommonsValidator.getErrorMessage can "see" the resource bundle?

Rahul Akolkar added a comment - 07/Nov/06 06:16 AM
Try again, it works for me. And adding <message-bundle> should, since the method in question looks up the application's message bundle for the "msg" attribute of (in this case, the custom) <validator>. The "message" attribute of <val:commonsValidator> gets priority, if present (which is nice, since it allows to change the "default" error message for the custom, or "built-in", validator).

On a separate note, we could do better than an NPE here, let me take a stab at that.

Repository Revision Date User Message
ASF #472002 Tue Nov 07 06:25:12 UTC 2006 rahul Slightly better error reporting when resource key cannot be resolved. Log and rethrow exception as MissingResourceException (contains problem key in message), rather than ignoring it (which induces a harder to debug NPE down the road). Fallout of SHALE-325.
Files Changed
MODIFY /shale/framework/trunk/shale-validator/src/main/resources/org/apache/shale/validator/resources/Bundle.properties
MODIFY /shale/framework/trunk/shale-validator/src/main/java/org/apache/shale/validator/CommonsValidator.java

Repository Revision Date User Message
ASF #476703 Sun Nov 19 04:38:12 UTC 2006 rahul Fix NPE in custom validator while looking up validation failure message. Remove related TODO.
SHALE-325
Files Changed
MODIFY /shale/framework/trunk/shale-apps/shale-usecases/src/main/webapp/validator/test.jsp
MODIFY /shale/framework/trunk/shale-apps/shale-usecases/src/main/webapp/WEB-INF/faces-config.xml

Rahul Akolkar added a comment - 19/Nov/06 04:41 AM
Per my last comment, I've committed a fix for this (one of Gary's suggestions), see:
  http://svn.apache.org/viewvc?view=rev&rev=476703
If there are no other comments in a couple of days, I will resolve this issue.

Craig McClanahan added a comment - 14/Dec/06 01:21 AM
Looks like no other comments, so I'll go ahead and close this as FIXED.


Craig McClanahan made changes - 14/Dec/06 01:21 AM
Field Original Value New Value
Status Open [ 1 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Rahul Akolkar made changes - 23/Jan/07 04:40 PM
Fix Version/s 1.0.4 [ 21790 ]
Fix Version/s 1.0.4-SNAPSHOT [ 21740 ]
Jeff Turner made changes - 09/Aug/07 07:17 AM
Workflow Struts [ 38900 ] Struts - editable closed status [ 42392 ]
Antonio Petrelli made changes - 08/Jan/09 08:57 AM
Workflow Struts - editable closed status [ 42392 ] Struts - editable closed status (temporary) [ 46296 ]
Antonio Petrelli made changes - 08/Jan/09 09:08 AM
Workflow Struts - editable closed status (temporary) [ 46296 ] Struts - editable closed status [ 52930 ]