Merge branch '5.1.x'

This commit is contained in:
Juergen Hoeller 2019-05-24 23:46:25 +02:00
commit 8f972a5a60
3 changed files with 30 additions and 6 deletions

View File

@ -74,7 +74,7 @@ public class SpringValidatorAdapterTests {
@Before
public void setupSpringValidatorAdapter() {
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}");
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} must be between {2} and {1}");
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}");
messageSource.addMessage("password", Locale.ENGLISH, "Password");
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)");
@ -100,7 +100,7 @@ public class SpringValidatorAdapterTests {
assertThat(errors.getFieldValue("password")).isEqualTo("pass");
FieldError error = errors.getFieldError("password");
assertThat(error).isNotNull();
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password is must be between 8 and 128");
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
assertThat(error.contains(ConstraintViolation.class)).isTrue();
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
}

View File

@ -169,7 +169,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()) {
@Override
public boolean shouldRenderDefaultMessage() {
return false;
return requiresMessageFormat(violation);
}
};
error.wrap(violation);
@ -182,7 +182,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
rejectedValue, false, errorCodes, errorArgs, violation.getMessage()) {
@Override
public boolean shouldRenderDefaultMessage() {
return false;
return requiresMessageFormat(violation);
}
};
error.wrap(violation);
@ -300,12 +300,36 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
* @param field the field that caused the binding error
* @return a corresponding {@code MessageSourceResolvable} for the specified field
* @since 4.3
* @see #getArgumentsForConstraint
*/
protected MessageSourceResolvable getResolvableField(String objectName, String field) {
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
return new DefaultMessageSourceResolvable(codes, field);
}
/**
* Indicate whether this violation's interpolated message has remaining
* placeholders and therefore requires {@link java.text.MessageFormat}
* to be applied to it. Called for a Bean Validation defined message
* (coming out {@code ValidationMessages.properties}) when rendered
* as the default message in Spring's MessageSource.
* <p>The default implementation considers a Spring-style "{0}" placeholder
* for the field name as an indication for {@link java.text.MessageFormat}.
* Any other placeholder or escape syntax occurrences are typically a
* mismatch, coming out of regex pattern values or the like. Note that
* standard Bean Validation does not support "{0}" style placeholders at all;
* this is a feature typically used in Spring MessageSource resource bundles.
* @param violation the Bean Validation constraint violation, including
* BV-defined interpolation of named attribute references in its message
* @return {@code true} if {@code java.text.MessageFormat} is to be applied,
* or {@code false} if the violation's message should be used as-is
* @since 5.1.8
* @see #getArgumentsForConstraint
*/
protected boolean requiresMessageFormat(ConstraintViolation<?> violation) {
return violation.getMessage().contains("{0}");
}
/**
* Extract the rejected value behind the given constraint violation,
* for exposure through the Spring errors representation.

View File

@ -71,7 +71,7 @@ public class SpringValidatorAdapterTests {
@Before
public void setupSpringValidatorAdapter() {
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}");
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} must be between {2} and {1}");
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}");
messageSource.addMessage("password", Locale.ENGLISH, "Password");
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)");
@ -97,7 +97,7 @@ public class SpringValidatorAdapterTests {
assertThat(errors.getFieldValue("password")).isEqualTo("pass");
FieldError error = errors.getFieldError("password");
assertThat(error).isNotNull();
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password is must be between 8 and 128");
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
assertThat(error.contains(ConstraintViolation.class)).isTrue();
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
}