[jboss-cvs] jboss-seam/src/main/org/jboss/seam/pages ...
Gavin King
gavin.king at jboss.com
Tue Jun 12 03:09:15 EDT 2007
User: gavin
Date: 07/06/12 03:09:15
Modified: src/main/org/jboss/seam/pages Param.java
Log:
validators and required for page parameters
Revision Changes Path
1.3 +77 -1 jboss-seam/src/main/org/jboss/seam/pages/Param.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Param.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/pages/Param.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- Param.java 1 May 2007 16:43:52 -0000 1.2
+++ Param.java 12 Jun 2007 07:09:15 -0000 1.3
@@ -4,9 +4,13 @@
package org.jboss.seam.pages;
import java.util.Map;
+import java.util.ResourceBundle;
+import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
import org.jboss.seam.core.Expressions.ValueExpression;
@@ -14,9 +18,15 @@
{
private final String name;
private ValueExpression valueExpression;
+
+ private boolean required;
+
private ValueExpression converterValueExpression;
private String converterId;
+ private ValueExpression validatorValueExpression;
+ private String validatorId;
+
public Param(String name)
{
this.name = name;
@@ -43,6 +53,22 @@
}
}
+ public Validator getValidator()
+ {
+ if (validatorId!=null)
+ {
+ return FacesContext.getCurrentInstance().getApplication().createValidator(converterId);
+ }
+ else if (validatorValueExpression!=null)
+ {
+ return (Validator) validatorValueExpression.getValue();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
public String getName()
{
return name;
@@ -118,10 +144,22 @@
* Get the current value of a page parameter from the request parameters
*/
public Object getValueFromRequest(FacesContext facesContext, Map<String, String[]> requestParameters)
+ throws ValidatorException
{
String[] parameterValues = requestParameters.get( getName() );
if (parameterValues==null || parameterValues.length==0)
{
+ if ( isRequired() )
+ {
+ String bundleName = facesContext.getApplication().getMessageBundle();
+ if (bundleName==null) bundleName = FacesMessage.FACES_MESSAGES;
+ ResourceBundle resourceBundle = facesContext.getApplication().getResourceBundle(facesContext, bundleName);
+ throw new ValidatorException( new FacesMessage(
+ FacesMessage.SEVERITY_ERROR,
+ resourceBundle.getString("javax.faces.component.UIInput.REQUIRED"),
+ resourceBundle.getString("javax.faces.component.UIInput.REQUIRED_detail")
+ ) );
+ }
return null;
}
if (parameterValues.length>1)
@@ -141,9 +179,47 @@
return null;
}
- return converter==null ?
+ Object value = converter==null ?
stringValue :
converter.getAsObject( facesContext, facesContext.getViewRoot(), stringValue );
+
+ Validator validator = getValidator();
+ if (validator!=null)
+ {
+ validator.validate( facesContext, facesContext.getViewRoot(), value );
+ }
+
+ return value;
+ }
+
+ public String getValidatorId()
+ {
+ return validatorId;
+ }
+
+ public void setValidatorId(String validatorId)
+ {
+ this.validatorId = validatorId;
+ }
+
+ public ValueExpression getValidatorValueExpression()
+ {
+ return validatorValueExpression;
+ }
+
+ public void setValidatorValueExpression(ValueExpression validatorValueExpression)
+ {
+ this.validatorValueExpression = validatorValueExpression;
+ }
+
+ public boolean isRequired()
+ {
+ return required;
+ }
+
+ public void setRequired(boolean required)
+ {
+ this.required = required;
}
}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list