This commit is contained in:
Keith Donald
2009-02-27 22:07:40 +00:00
parent fa97580264
commit 8a1eb749f5
5 changed files with 177 additions and 4 deletions

View File

@@ -66,6 +66,7 @@ public class DefaultValidationFailureMessageResolverFactory implements Validatio
ConversionService conversionService) {
Assert.notNull(expressionParser, "The expressionParser is required");
this.expressionParser = expressionParser;
this.conversionService = conversionService;
}
/**
@@ -92,6 +93,8 @@ public class DefaultValidationFailureMessageResolverFactory implements Validatio
private static final String LABEL_ARGUMENT = "label";
private static final String VALUE_ARGUMENT = "value";
private ValidationFailure failure;
private ValidationFailureModelContext modelContext;
@@ -116,7 +119,11 @@ public class DefaultValidationFailureMessageResolverFactory implements Validatio
} else {
label = appendLabelPrefix().append(CODE_SEPARATOR).append(modelContext.getObjectName()).toString();
}
stringArgs.put(LABEL_ARGUMENT, new DefaultMessageSourceResolvable(label));
stringArgs.put(LABEL_ARGUMENT, new DefaultMessageSourceResolvable(new String[] { label }, failure
.getPropertyName()));
}
if (!stringArgs.containsKey(VALUE_ARGUMENT) && failure.getPropertyName() != null) {
stringArgs.put(VALUE_ARGUMENT, modelContext.getInvalidUserValue());
}
if (failure.getArguments() != null) {
Iterator it = failure.getArguments().entrySet().iterator();
@@ -140,7 +147,8 @@ public class DefaultValidationFailureMessageResolverFactory implements Validatio
}
private String[] buildCodes() {
String constraintMessageCode = appendMessageCodePrefix().append(failure.getConstraint()).toString();
String constraintMessageCode = appendMessageCodePrefix().append(CODE_SEPARATOR).append(
failure.getConstraint()).toString();
if (failure.getPropertyName() != null) {
String propertyConstraintMessageCode = appendMessageCodePrefix().append(CODE_SEPARATOR).append(
modelContext.getObjectName()).append(CODE_SEPARATOR).append(failure.getPropertyName()).append(

View File

@@ -19,6 +19,8 @@ import java.util.Map;
import java.util.TreeMap;
import org.springframework.binding.message.Severity;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.DefaultMessageSourceResolvable;
/**
* A builder that provides a fluent interface for configuring a Validation failure. Example:
@@ -80,7 +82,9 @@ public class ValidationFailureBuilder {
}
/**
* Sets the failure to be of warning severity.
* Adds a failure message argument.
* @param name the arg name
* @param value the arg value
* @return this, for fluent call chaining
*/
public ValidationFailureBuilder arg(String name, Object value) {
@@ -91,6 +95,18 @@ public class ValidationFailureBuilder {
return this;
}
/**
* Adds a failure message argument whose value is also message source resolvable. Use this when the argument value
* itself needs to be localized.
* @param name the arg name
* @param code the code that will be used to resolve the arg
* @see MessageSourceResolvable
* @return this, for fluent call chaining
*/
public ValidationFailureBuilder resolvableArg(String name, String code) {
return arg(name, new DefaultMessageSourceResolvable(code));
}
/**
* Build the ValidationFailure. Called after setting builder properties.
* @return this, for fluent call chaining

View File

@@ -12,14 +12,19 @@ public class ValidationFailureModelContext {
private String propertyTypeConverter;
private Object invalidValue;
/**
* Creates a new validation model context.
* @param objectName the object name
* @param invalidValue the invalid value the user entered
* @param propertyType the property type (may be null)
* @param propertyTypeConverter the id of the property type converter (may be null)
*/
public ValidationFailureModelContext(String objectName, Class propertyType, String propertyTypeConverter) {
public ValidationFailureModelContext(String objectName, Object invalidValue, Class propertyType,
String propertyTypeConverter) {
this.objectName = objectName;
this.invalidValue = invalidValue;
this.propertyType = propertyType;
this.propertyTypeConverter = propertyTypeConverter;
}
@@ -31,6 +36,13 @@ public class ValidationFailureModelContext {
return objectName;
}
/**
* The user entered value.
*/
public Object getInvalidUserValue() {
return invalidValue;
}
/**
* When reporting a property validation failure, the type of the property that failed to validate.
*/