BeanValidator was modified to throw InvalidValueException with array of causes instead of exception with one message including HTML. This way AbstractErrorMessage is able to create correct error notification message with multiple lines. Change-Id: I414189f56ac282daad9dd3fe58d13fd99108c479tags/7.2.0.beta1
@@ -17,8 +17,6 @@ | |||
package com.vaadin.data.validator; | |||
import java.io.Serializable; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Locale; | |||
import java.util.Set; | |||
@@ -115,7 +113,9 @@ public class BeanValidator implements Validator { | |||
Set<?> violations = getJavaxBeanValidator().validateValue(beanClass, | |||
propertyName, value); | |||
if (violations.size() > 0) { | |||
List<String> exceptions = new ArrayList<String>(); | |||
InvalidValueException[] causes = new InvalidValueException[violations | |||
.size()]; | |||
int i = 0; | |||
for (Object v : violations) { | |||
final ConstraintViolation<?> violation = (ConstraintViolation<?>) v; | |||
String msg = getJavaxBeanValidatorFactory() | |||
@@ -123,16 +123,11 @@ public class BeanValidator implements Validator { | |||
violation.getMessageTemplate(), | |||
new SimpleContext(value, violation | |||
.getConstraintDescriptor()), locale); | |||
exceptions.add(msg); | |||
causes[i] = new InvalidValueException(msg); | |||
++i; | |||
} | |||
StringBuilder b = new StringBuilder(); | |||
for (int i = 0; i < exceptions.size(); i++) { | |||
if (i != 0) { | |||
b.append("<br/>"); | |||
} | |||
b.append(exceptions.get(i)); | |||
} | |||
throw new InvalidValueException(b.toString()); | |||
throw new InvalidValueException(null, causes); | |||
} | |||
} | |||
@@ -4,6 +4,7 @@ import javax.validation.constraints.Digits; | |||
import javax.validation.constraints.Max; | |||
import javax.validation.constraints.Min; | |||
import javax.validation.constraints.NotNull; | |||
import javax.validation.constraints.Pattern; | |||
import javax.validation.constraints.Size; | |||
public class BeanToValidate { | |||
@@ -21,6 +22,10 @@ public class BeanToValidate { | |||
@Digits(integer = 3, fraction = 2) | |||
private String decimals; | |||
@Pattern(regexp = "V*", message = "Must start with letter V") | |||
@Size(min = 3, max = 6, message = "Must contain 3 - 6 letters") | |||
private String nickname; | |||
public String getFirstname() { | |||
return firstname; | |||
} | |||
@@ -53,4 +58,12 @@ public class BeanToValidate { | |||
this.decimals = decimals; | |||
} | |||
public String getNickname() { | |||
return nickname; | |||
} | |||
public void setNickname(String nickname) { | |||
this.nickname = nickname; | |||
} | |||
} |
@@ -8,12 +8,15 @@ import javax.validation.constraints.Max; | |||
import javax.validation.constraints.Min; | |||
import javax.validation.constraints.NotNull; | |||
import javax.validation.constraints.Past; | |||
import javax.validation.constraints.Pattern; | |||
import javax.validation.constraints.Size; | |||
public class PersonWithBeanValidationAnnotations { | |||
@NotNull | |||
@Size(min = 5, max = 20) | |||
@Pattern(regexp = "A.*") | |||
private String firstName; | |||
@NotNull | |||
private String lastName; | |||
@@ -1,5 +1,6 @@ | |||
package com.vaadin.tests.server.validation; | |||
import org.junit.Assert; | |||
import org.junit.Test; | |||
import com.vaadin.data.Validator.InvalidValueException; | |||
@@ -54,4 +55,31 @@ public class TestBeanValidation { | |||
validator.validate("123.45"); | |||
} | |||
@Test | |||
public void testBeanValidationException_OneValidationError() { | |||
InvalidValueException[] causes = null; | |||
BeanValidator validator = new BeanValidator(BeanToValidate.class, | |||
"lastname"); | |||
try { | |||
validator.validate(null); | |||
} catch (InvalidValueException e) { | |||
causes = e.getCauses(); | |||
} | |||
Assert.assertEquals(1, causes.length); | |||
} | |||
@Test | |||
public void testBeanValidationsException_TwoValidationErrors() { | |||
InvalidValueException[] causes = null; | |||
BeanValidator validator = new BeanValidator(BeanToValidate.class, | |||
"nickname"); | |||
try { | |||
validator.validate("A"); | |||
} catch (InvalidValueException e) { | |||
causes = e.getCauses(); | |||
} | |||
Assert.assertEquals(2, causes.length); | |||
} | |||
} |
@@ -7,7 +7,6 @@ import com.vaadin.data.util.BeanItem; | |||
import com.vaadin.tests.components.TestBase; | |||
import com.vaadin.tests.data.bean.Address; | |||
import com.vaadin.tests.data.bean.Country; | |||
import com.vaadin.tests.data.bean.Person; | |||
import com.vaadin.tests.data.bean.PersonWithBeanValidationAnnotations; | |||
import com.vaadin.tests.data.bean.Sex; | |||
import com.vaadin.tests.util.Log; | |||
@@ -90,8 +89,10 @@ public class FieldBinderWithBeanValidation extends TestBase { | |||
p)); | |||
} | |||
public static Person getPerson(FieldGroup binder) { | |||
return ((BeanItem<Person>) binder.getItemDataSource()).getBean(); | |||
public static PersonWithBeanValidationAnnotations getPerson( | |||
FieldGroup binder) { | |||
return ((BeanItem<PersonWithBeanValidationAnnotations>) binder | |||
.getItemDataSource()).getBean(); | |||
} | |||
@Override |