This commit is contained in:
Keith Donald
2008-11-12 17:18:49 +00:00
parent bdc205f0a6
commit db81d309d1
3 changed files with 44 additions and 9 deletions

View File

@@ -44,9 +44,9 @@ public interface ValidationContext {
public String getUserEvent();
/**
* Obtain the value entered by the current user in a field. This value can then be validated.
* @param field the name of the field
* @return the value the user entered in the field
* Obtain the value entered by the current user in the UI field bound to the property provided.
* @param property the name of a bound property
* @return the value the user entered in the field bound to the property
*/
public Object getUserValue(String field);
public Object getUserValue(String property);
}

View File

@@ -1,7 +1,11 @@
package org.springframework.webflow.validation;
import java.security.Principal;
import java.util.List;
import org.springframework.binding.mapping.MappingResult;
import org.springframework.binding.mapping.MappingResults;
import org.springframework.binding.mapping.MappingResultsCriteria;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.webflow.execution.RequestContext;
@@ -12,7 +16,9 @@ public class DefaultValidationContext implements ValidationContext {
private String eventId;
public DefaultValidationContext(RequestContext requestContext, String eventId) {
private MappingResults mappingResults;
public DefaultValidationContext(RequestContext requestContext, String eventId, MappingResults mappingResults) {
this.requestContext = requestContext;
this.eventId = eventId;
}
@@ -35,8 +41,32 @@ public class DefaultValidationContext implements ValidationContext {
return requestContext.getExternalContext().getCurrentUser();
}
public Object getUserValue(String field) {
throw new UnsupportedOperationException("Not yet implemented");
public Object getUserValue(String property) {
if (mappingResults != null) {
List results = mappingResults.getResults(new PropertyMappingResult(property));
if (!results.isEmpty()) {
MappingResult result = (MappingResult) results.get(0);
return result.getOriginalValue();
}
}
return null;
}
private static class PropertyMappingResult implements MappingResultsCriteria {
private String field;
public PropertyMappingResult(String field) {
this.field = field;
}
public boolean test(MappingResult result) {
if (field.equals(result.getMapping().getTargetExpression().getExpressionString())) {
return true;
} else {
return false;
}
}
}
}

View File

@@ -38,10 +38,15 @@ import org.springframework.webflow.execution.RequestContext;
public class ValidationHelper {
private final Object model;
private final RequestContext requestContext;
private final String eventId;
private final String modelName;
private final ExpressionParser expressionParser;
private final MappingResults mappingResults;
/**
@@ -101,7 +106,7 @@ public class ValidationHelper {
new Class[] { ValidationContext.class });
if (validateMethod != null) {
ReflectionUtils.invokeMethod(validateMethod, model, new Object[] { new DefaultValidationContext(
requestContext, eventId) });
requestContext, eventId, mappingResults) });
validationInvoked = true;
} else {
validateMethod = ReflectionUtils.findMethod(model.getClass(), validateMethodName,
@@ -125,7 +130,7 @@ public class ValidationHelper {
model.getClass(), ValidationContext.class });
if (validateMethod != null) {
ReflectionUtils.invokeMethod(validateMethod, validator, new Object[] { model,
new DefaultValidationContext(requestContext, eventId) });
new DefaultValidationContext(requestContext, eventId, mappingResults) });
validationInvoked = true;
} else {
validateMethod = ReflectionUtils.findMethod(validator.getClass(), validateMethodName, new Class[] {