Author: alexsmirnov
Date: 2009-11-16 18:56:33 -0500 (Mon, 16 Nov 2009)
New Revision: 15888
Added:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/FacesValidatorTest.java
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/include/pagesList.xhtml
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/GraphValidatorBean.java
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/MinMaxBean.java
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/graphValidation.xhtml
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java
Log:
https://jira.jboss.org/jira/browse/RF-7636, fix graphValidator example.
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/include/pagesList.xhtml
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/include/pagesList.xhtml 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/include/pagesList.xhtml 2009-11-16
23:56:33 UTC (rev 15888)
@@ -7,7 +7,6 @@
xmlns:c="http://java.sun.com/jstl/core"
<ui:composition>
- <h:form>
<h:dataTable value="#{pages.xhtmlPages}" var="page">
<h:column>
<h:outputLink value="#{page.url}">
@@ -15,6 +14,5 @@
</h:outputLink>
</h:column>
</h:dataTable>
- </h:form>
</ui:composition>
</html>
\ No newline at end of file
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/GraphValidatorBean.java
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/GraphValidatorBean.java 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/GraphValidatorBean.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -11,17 +11,18 @@
* @author asmirnov
*
*/
-public class GraphValidatorBean {
+public class GraphValidatorBean implements Cloneable {
@Min(0)
@Max(10)
private int first ;
- @Min(0)
- @Max(10)
+ @Min(value=5,message="Value {0} should be more than {value}")
+ @Max(15)
private int second ;
+
@Min(0)
- @Max(10)
+ @Max(20)
private int third ;
private String actionResult;
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/MinMaxBean.java
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/MinMaxBean.java 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/jsr303src/org/richfaces/example/MinMaxBean.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -16,7 +16,7 @@
private String text;
- @Min(2)
+ @Min(value=2,message="Value {0} should be more than {value}")
@Max(10)
private int intValue;
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/layout/template.xhtml 2009-11-16
23:56:33 UTC (rev 15888)
@@ -1,59 +1,47 @@
-<f:view
xmlns:ui="http://java.sun.com/jsf/facelets"
+<rich:page
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
-
xmlns:s="http://jboss.com/products/seam/taglib"
-
xmlns:a="http://richfaces.org/a4j"
-
xmlns:rich="http://richfaces.org/rich"
- locale="#{locale.current}">
-<a:page pageTitle="#{title}" format="xhtml">
- <a:loadStyle src="/css/grids.css" />
- <a:loadStyle src="resource://css/panel.xcss" />
- <a:loadStyle src="resource://css/table.xcss" />
- <div id="doc3" class="yui-t3 dr-pnl">
- <div id="hd" class="dr-pnl-h">
- <h1>RichFaces Validation components examples</h1>
- <ui:insert name="header" /></div>
- <div id="bd">
- <div class="yui-b">
- <!-- YOUR NAVIGATION GOES HERE -->
- <ui:include src="/include/pagesList.xhtml" />
- <h:form>
- <h:selectOneMenu value="#{locale.language}">
- <f:selectItem itemLabel="English" itemValue="en"/>
- <f:selectItem itemLabel="Deutsch" itemValue="de"/>
- <f:selectItem itemLabel="Francais" itemValue="fr"/>
-</h:selectOneMenu>
-<h:commandButton action="#{locale.select}"
value="ChangeLanguage"/>
-</h:form>
- </div>
- <div class="yui-main">
- <div class="yui-b">
- <!-- YOUR DATA GOES HERE -->
- <rich:tabPanel binding="#{tabNavigation.tabPanel}"
switchType="ajax">
- <rich:tab name="usage" label="Usage">
- <ui:insert name="description" />
- <ui:insert name="content" />
- </rich:tab>
- <rich:tab name="page" label="Page Source">
- <rich:insert content="#{contentBean.content}"
highlight="xhtml"/>
- </rich:tab>
- <rich:tab name="java" label="Java Source"
rendered="#{!empty javaBean}">
- <rich:insert src="/src/#{javaBean}"
highlight="java"/>
- </rich:tab>
- </rich:tabPanel>
- </div>
- </div>
- </div>
- <div id="ft" class="dr-table-footer">
- <div class="yui-ge dr-table-footercell">
- <div class="yui-u first">
- Built with RichFaces #{a4j.version}
- </div>
- <div class="yui-u" style="text-align:right">
- © 2007 Exadel, Inc.
- </div>
- </div>
- </div>
- </div>
-</a:page>
-</f:view>
\ No newline at end of file
+
xmlns:a="http://richfaces.org/a4j"
+
xmlns:rich="http://richfaces.org/rich" pageTitle="#{title}"
+ theme="simple" markupType="html">
+ <f:facet name="header">
+ <h1>RichFaces Validation components examples</h1>
+ </f:facet>
+ <f:facet name="subheader">
+ <ui:insert name="header" />
+ </f:facet>
+ <f:facet name="sidebar">
+ <h:panelGroup>
+ <!-- YOUR NAVIGATION GOES HERE -->
+ <ui:include src="/include/pagesList.xhtml" />
+ <h:form>
+ <h:selectOneMenu value="#{locale.language}">
+ <f:selectItem itemLabel="English"
itemValue="en" />
+ <f:selectItem itemLabel="Deutsch"
itemValue="de" />
+ <f:selectItem itemLabel="Francais"
itemValue="fr" />
+ </h:selectOneMenu>
+ <h:commandButton action="#{locale.select}"
+ value="ChangeLanguage" />
+ </h:form>
+ </h:panelGroup>
+ </f:facet>
+ <!-- YOUR DATA GOES HERE -->
+ <rich:tabPanel binding="#{tabNavigation.tabPanel}"
switchType="ajax">
+ <rich:tab name="usage" label="Usage">
+ <div><ui:insert name="description" /></div>
+ <div><ui:insert name="content" /></div>
+ </rich:tab>
+ <rich:tab name="page" label="Page Source">
+ <rich:insert content="#{contentBean.content}"
+ highlight="xhtml" />
+ </rich:tab>
+ <rich:tab name="java" label="Java Source"
+ rendered="#{!empty javaBean}">
+ <rich:insert src="/src/#{javaBean}" highlight="java"
/>
+ </rich:tab>
+ </rich:tabPanel>
+ <f:facet name="footer">
+ <h:panelGroup>Built with RichFaces #{a4j.version}
+ (C) 2009 Exadel, Inc.</h:panelGroup>
+ </f:facet>
+</rich:page>
Modified:
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/graphValidation.xhtml
===================================================================
---
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/graphValidation.xhtml 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/samples/beanValidatorSample/src/main/webapp/pages/graphValidation.xhtml 2009-11-16
23:56:33 UTC (rev 15888)
@@ -13,13 +13,15 @@
<!-- Page header -->
<ui:define name="header">
<h1><rich:graphValidator> usage</h1>
- <p>In that sampe <rich:graphValidator> component appends
JSR-299 or Hibernate validators to all enclosed input components
+ </ui:define>
+ <!-- content -->
+ <ui:define name="description">
+ <p>In that sampe <rich:graphValidator> component appends
JSR-303 or Hibernate validators to all enclosed input components
that check restrictions for field values, and, in addition, validates whole
bean.</p>
<p>JSF bean fields are annotated with @Min/@Max restrictions and getter for
the 'total' attribute is also annotated by the @Max</p>
<p>As a result, even valid field values would make whole bean an invalid.
For example, values '2','7','15' are valid for fields values but
their sum exceed maximum total value '20'</p>
<p>Validator assignes new values to a cloned bean instance hence model is
not updated with invalid values.</p>
- </ui:define>
- <!-- content -->
+ </ui:define>
<ui:define name="content">
<h:form id="form">
<rich:graphValidator value="#{graphValidatorBean}"
id="validator" profiles="javax.validation.groups.Default">
@@ -35,12 +37,12 @@
<h:outputText value="#{graphValidatorBean.first}"/>
<h:outputLabel for="value1" value="Second value,integer from 5 to
15:" />
- <h:inputText id="value1" value="#{graphValidatorBean.second}"
label="First" />
+ <h:inputText id="value1" value="#{graphValidatorBean.second}"
label="Second" />
<rich:message for="value1"/>
<h:outputText value="#{graphValidatorBean.second}"/>
<h:outputLabel for="value2" value="Third value,integer from 0 to
20:" />
- <h:inputText id="value2" value="#{graphValidatorBean.third}"
label="First" />
+ <h:inputText id="value2" value="#{graphValidatorBean.third}"
label="Third" />
<rich:message for="value2"/>
<h:outputText value="#{graphValidatorBean.third}"/>
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -149,8 +150,12 @@
Class<?> type = valueExpression.getType(new ELContextWrapper(initialELContext,
capturingELResolver));
if(null != type) {
validatorState = new GraphValidatorState();
- Method method = value.getClass().getDeclaredMethod("clone");
- method.setAccessible(true);
+ Method method = getCloneMethod(value.getClass());
+ if(!Modifier.isPublic(method.getModifiers())){
+ // Method Object#clone() is protected by default. Make it public
+ // unless developer did it.
+ method.setAccessible(true);
+ }
validatorState.cloned = method.invoke(value);
validatorState.base = capturingELResolver.getBase();
validatorState.property = capturingELResolver.getProperty();
@@ -174,6 +179,18 @@
}
}
+ private Method getCloneMethod(Class<?> clazz) throws NoSuchMethodException {
+ try {
+ return clazz.getDeclaredMethod("clone");
+ } catch( NoSuchMethodException e){
+ if(null != clazz.getSuperclass()){
+ return getCloneMethod(clazz.getSuperclass());
+ } else {
+ throw e;
+ }
+ }
+ }
+
protected String getStateId(FacesContext context) {
String stateId = STATE_ATTRIBUTE_PREFIX+getClientId(context);
return stateId;
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -25,6 +25,7 @@
private static final Class[] DEFAULT_PROFILE = new Class[] {};
private final ValidatorFactory validatorFactory;
+ private ValidatorContext validatorContext;
BeanValidator() {
// Enforce class to load
@@ -33,6 +34,10 @@
//
https://jira.jboss.org/jira/browse/RF-7226
validatorFactory = Validation
.buildDefaultValidatorFactory();
+ validatorContext = validatorFactory.usingContext();
+ MessageInterpolator jsfMessageInterpolator = new JsfMessageInterpolator(
+ validatorFactory.getMessageInterpolator());
+ validatorContext.messageInterpolator(jsfMessageInterpolator);
}
/*
@@ -107,30 +112,25 @@
}
protected Validator getValidator(FacesContext facesContext) {
- ValidatorContext validatorContext = validatorFactory.usingContext();
- MessageInterpolator jsfMessageInterpolator = new JsfMessageInterpolator(
- calculateLocale(facesContext), validatorFactory.getMessageInterpolator());
- validatorContext.messageInterpolator(jsfMessageInterpolator);
Validator beanValidator = validatorContext.getValidator();
return beanValidator;
}
private static class JsfMessageInterpolator implements MessageInterpolator {
- private Locale locale;
private MessageInterpolator delegate;
- public JsfMessageInterpolator(Locale locale,
+ public JsfMessageInterpolator(
MessageInterpolator delegate) {
- this.locale = locale;
this.delegate = delegate;
}
public String interpolate(String messageTemplate, Context context) {
+ Locale locale = ObjectValidator.calculateLocale(FacesContext.getCurrentInstance());
if (null != locale) {
return delegate.interpolate(messageTemplate, context,
- this.locale);
+ locale);
} else {
return delegate.interpolate(messageTemplate, context);
}
@@ -138,9 +138,10 @@
public String interpolate(String messageTemplate, Context context,
Locale locale) {
- if (null != locale) {
+ Locale faceslocale =
ObjectValidator.calculateLocale(FacesContext.getCurrentInstance());
+ if (null != faceslocale) {
return delegate.interpolate(messageTemplate, context,
- this.locale);
+ faceslocale);
} else {
return delegate.interpolate(messageTemplate, context, locale);
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -21,6 +21,8 @@
package org.richfaces.validator;
import java.io.Serializable;
+import java.text.MessageFormat;
+import java.util.Locale;
import java.util.Set;
import javax.el.ELException;
@@ -99,16 +101,20 @@
ValueExpression valueExpression = component
.getValueExpression("value");
if (null != valueExpression) {
- // TODO - check EL Exceptions ?
- String[] messages = HibernateValidator.getInstance(context)
+ String[] messages = ObjectValidator.getInstance(context)
.validate(context, valueExpression, convertedValue, getProfiles());
if (null != messages) {
input.setValid(false);
+ Object label = getLabel(context, component);
+ Locale locale = context.getViewRoot().getLocale();
// send all validation messages.
for (String msg : messages) {
- // TODO - create Summary message ?
- String summaryString = getSummary()!=null?getSummary():msg;
-
+ //
https://jira.jboss.org/jira/browse/RF-7636 -
+ // format values.
+ msg = formatMessage(msg, locale, label,convertedValue); // create Summary message
?
+ String summaryString = getSummary() != null ? getSummary()
+ : msg;
+ summaryString = formatMessage(summaryString, locale, label,convertedValue);
context.addMessage(component.getClientId(context), new FacesMessage(
FacesMessage.SEVERITY_ERROR, summaryString , msg));
}
@@ -120,9 +126,27 @@
}
}
+ static String formatMessage(String msg, Locale locale, Object... messageParams) {
+ if (msg.contains("{")) {
+ MessageFormat messageFormat = new MessageFormat(
+ msg, locale);
+ msg = messageFormat.format(messageParams);
+
+ }
+ return msg;
+ }
+
+ static Object getLabel(FacesContext context, UIComponent component) {
+ Object label = component.getAttributes().get("label");
+ if(null == label || 0 == label.toString().length()){
+ label = component.getClientId(context);
+ }
+ return label;
+ }
+
public String[] validateGraph(FacesContext context, UIComponent component,
Object value, Object profiles) throws ValidatorException {
- ObjectValidator beanValidator = HibernateValidator.getInstance(context);
+ ObjectValidator beanValidator = ObjectValidator.getInstance(context);
String[] messages = beanValidator.validateGraph(context,
value,AjaxRendererUtils.asSet(profiles));
return messages;
}
Modified:
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java 2009-11-16
18:20:11 UTC (rev 15887)
+++
branches/community/3.3.X/ui/beanValidator/src/main/java/org/richfaces/validator/ObjectValidator.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -45,7 +45,7 @@
*
* @return
*/
- protected static ObjectValidator createInstance() {
+ static ObjectValidator createInstance() {
// TODO - get instance class name from a "META-INF/service"
// If the Seam framework is active, org.jboss.seam.core.Validators
// component should be used.
@@ -119,7 +119,6 @@
context, elContext.getELResolver(),profiles);
ELContextWrapper wrappedElContext = new ELContextWrapper(elContext,
validationResolver);
- // TODO - handle ELExceptions ?
try {
target.setValue(wrappedElContext, value);
} catch (ELException e) {
@@ -133,16 +132,6 @@
return validationMessages;
}
- protected Locale calculateLocale(FacesContext context) {
- if (null == context.getViewRoot()) {
- throw new FacesException(VIEW_ROOT_IS_NOT_INITIALIZED);
- } else if (null == context.getViewRoot().getLocale()) {
- throw new FacesException(LOCALE_IS_NOT_SET);
- }
- Locale locale = context.getViewRoot().getLocale();
- return locale;
- }
-
/**
* Validate bean property for a new value.
* @param facesContext TODO
@@ -160,8 +149,17 @@
protected abstract String[] validate(FacesContext facesContext, Object base,
String property, Object value, Set<String> profiles);
- protected ResourceBundle getResourceBundle(FacesContext facesContext, String name) {
- // TODO - cache resource bundles.
+ static Locale calculateLocale(FacesContext context) {
+ if (null == context.getViewRoot()) {
+ throw new FacesException(VIEW_ROOT_IS_NOT_INITIALIZED);
+ } else if (null == context.getViewRoot().getLocale()) {
+ throw new FacesException(LOCALE_IS_NOT_SET);
+ }
+ Locale locale = context.getViewRoot().getLocale();
+ return locale;
+ }
+
+ static ResourceBundle getResourceBundle(FacesContext facesContext, String name) {
ResourceBundle bundle = null;
if (null != facesContext) {
Application application = facesContext.getApplication();
@@ -177,7 +175,7 @@
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (null == classLoader) {
- classLoader = this.getClass().getClassLoader();
+ classLoader = ObjectValidator.class.getClassLoader();
}
try {
bundle = ResourceBundle.getBundle(name, calculateLocale(facesContext),
Added:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/FacesValidatorTest.java
===================================================================
---
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/FacesValidatorTest.java
(rev 0)
+++
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/FacesValidatorTest.java 2009-11-16
23:56:33 UTC (rev 15888)
@@ -0,0 +1,48 @@
+package org.richfaces.validator;
+
+import java.util.Locale;
+
+import javax.faces.component.UIInput;
+import javax.faces.component.html.HtmlInputText;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+
+public class FacesValidatorTest extends AbstractAjax4JsfTestCase {
+
+ public FacesValidatorTest(String name) {
+ super(name);
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testValidate() {
+
+ }
+
+ public void testFormatMessage() {
+ String message = FacesBeanValidator.formatMessage("foo {0}", Locale.ENGLISH,
"bar");
+ assertEquals("foo bar", message);
+ }
+
+ public void testGetLabel() {
+ UIInput input = new UIInput();
+ input.setId("foo");
+ facesContext.getViewRoot().getChildren().add(input);
+ assertEquals("foo", FacesBeanValidator.getLabel(facesContext, input));
+ }
+
+ public void testGetLabel1() {
+ HtmlInputText input = new HtmlInputText();
+ input.setId("foo");
+ input.setLabel("bar");
+ facesContext.getViewRoot().getChildren().add(input);
+ assertEquals("bar", FacesBeanValidator.getLabel(facesContext, input));
+ }
+
+}
Property changes on:
branches/community/3.3.X/ui/beanValidator/src/test/java/org/richfaces/validator/FacesValidatorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain