Skip to content
Browse files

Testing BeanFormValidator (going to remove it after this commit)

  • Loading branch information...
1 parent ca62b51 commit 1c6d96962591623bce4a3747b6cfb08ec12f2b4a Ben Tilford committed
View
12 core/src/main/java/wicket/validation/BeanFormValidator.java
@@ -20,6 +20,8 @@
* To change this template use File | Settings | File Templates.
*/
public class BeanFormValidator<T> extends AbstractFormValidator {
+ private static final long serialVersionUID = 1L;
+
private static final Logger logger = LoggerFactory.getLogger(BeanFormValidator.class.getName());
private final Class<?>[] groups;
private final Class<T> modelType;
@@ -41,10 +43,10 @@ protected BeanFormValidator(final Class<T> modelType, final Class<?>[] groups) {
@Override
public void validate(Form<?> form) {
- //crazy wicket wildcard generics
+ //crazy wicket wildcard generics ... Form<?> should be Form<T>
if (modelType.isAssignableFrom(form.getModelObject().getClass())) {
- Set<ConstraintViolation<T>> violations = validateValue((T) form.getModelObject());
- for (ConstraintViolation<T> violation : violations) {
+ Set<ConstraintViolation<Object>> violations = validateValue(form.getModelObject());
+ for (ConstraintViolation<Object> violation : violations) {
form.error(new StringResourceModel(violation.getMessageTemplate(), form, null).getString());
logger.debug("validation error \"" + violation.getMessageTemplate() + "\"");
}
@@ -55,7 +57,7 @@ public void validate(Form<?> form) {
}
- Set<ConstraintViolation<T>> validateValue(T value) {
+ Set<ConstraintViolation<Object>> validateValue(Object value) {
return getValidator().validate(value, groups);
}
@@ -71,4 +73,6 @@ protected Validator getValidator() {
public static <T> BeanFormValidator<T> create(final Class<T> modelType, final Class<?>... groups) {
return new BeanFormValidator<T>(modelType, groups);
}
+
+
}
View
4 core/src/main/java/wicket/validation/PropertyModelValidationListener.java
@@ -71,12 +71,12 @@ boolean applyValidator(Component component) {
break;
}
}
- logger.debug("Filter result ==" + apply);
+ logger.trace("Filter result ==" + apply);
}
apply = apply && (formComponent.getModel() instanceof AbstractPropertyModel);
}
- logger.debug("PropertyModelValidationListener.applyValidator == " + apply);
+ logger.trace("PropertyModelValidationListener.applyValidator == " + apply);
return apply;
}
}
View
27 demo/src/main/java/wicket/validation/demo/HomePage.html
@@ -9,10 +9,11 @@
<span wicket:id="message">message will be here</span>
<form wicket:id="person">
- <div wicket:id="feedback"/>
-
<h1>BeanValidator</h1>
+
+ <div wicket:id="feedback"/>
+
<label for="name">
<wicket:message key="person.name">Name</wicket:message>
</label>
@@ -55,47 +56,49 @@
<form wicket:id="formValidator">
- <div wicket:id="feedback2"/>
-
<h1>BeanFormValidator</h1>
+
+ <div wicket:id="feedback"/>
+
+
<label for="name2">
<wicket:message key="person.name">Name</wicket:message>
</label>
- <input wicket:id="name2" name="name2"/>
+ <input wicket:id="name" name="name2"/>
<br/>
<label for="address.lineOne2">
<wicket:message key="address.lineOne">Address</wicket:message>
</label>
- <input wicket:id="address.lineOne2" name="address.lineOne2"/>
- <input wicket:id="address.lineTwo2" name="address.lineTwo2"/>
+ <input wicket:id="address.lineOne" name="address.lineOne2"/>
+ <input wicket:id="address.lineTwo" name="address.lineTwo2"/>
<br/>
<label for="address.city2">
<wicket:message key="address.city">City</wicket:message>
</label>
- <input wicket:id="address.city2" name="address.city2"/>
+ <input wicket:id="address.city" name="address.city2"/>
<br/>
<label for="address.region2">
<wicket:message key="address.region">State</wicket:message>
</label>
- <input wicket:id="address.region2" name="address.region2"/>
+ <input wicket:id="address.region" name="address.region2"/>
<br/>
<label for="address.country2">
<wicket:message key="address.country">Country</wicket:message>
</label>
- <input wicket:id="address.country2" name="address.country2"/>
+ <input wicket:id="address.country" name="address.country2"/>
<br/>
<label for="address.postalCode2">
<wicket:message key="address.postalCode">Zip Code</wicket:message>
</label>
- <input wicket:id="address.postalCode2" name="address.postalCode2"/>
+ <input wicket:id="address.postalCode" name="address.postalCode2"/>
<br/>
- <input wicket:id="submit2" type="submit" value="Submit"/>
+ <input wicket:id="submit" type="submit" value="Submit"/>
</form>
</body>
View
63 demo/src/main/java/wicket/validation/demo/HomePage.java
@@ -10,6 +10,8 @@
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import wicket.validation.BeanFormValidator;
import wicket.validation.demo.beans.Address;
import wicket.validation.demo.beans.Person;
@@ -21,6 +23,8 @@
private static final long serialVersionUID = 1L;
+ private static final Logger logger = LoggerFactory.getLogger(HomePage.class.getName());
+
// TODO Add any page properties or variables here
/**
@@ -58,26 +62,51 @@ public void onSubmit() {
this.add(personForm);
- Person person2 = new Person();
+ final Person person2 = new Person();
person2.setAddress(new Address());
- Form<Person> formValidator = new Form<Person>("formValidator", Model.of(person2));
+ final TextField<String> name = new TextField<String>("name", Model.of(person2.getName()));
+ final TextField<String> addressLineOne = new TextField<String>("address.lineOne", Model.of(person2.getAddress().getLineOne()));
+ final TextField<String> addressLineTwo = new TextField<String>("address.lineTwo", Model.of(person2.getAddress().getLineTwo()));
+ final TextField<String> addressCity = new TextField<String>("address.city", Model.of(person2.getAddress().getCity()));
+ final TextField<String> addressRegion = new TextField<String>("address.region", Model.of(person2.getAddress().getRegion()));
+ final TextField<String> addressCountry = new TextField<String>("address.country", Model.of(person2.getAddress().getCountry()));
+ final TextField<String> addressPostalCode = new TextField<String>("address.postalCode", Model.of(person2.getAddress().getPostalCode()));
+
+
+ final Form<Person> formValidator = new Form<Person>("formValidator", Model.of(person2)) {
+
+
+ @Override
+ protected void onValidate() {
+ this.getModelObject().setName(name.getValue());
+ this.getModelObject().getAddress().setLineOne(addressLineOne.getValue());
+ this.getModelObject().getAddress().setLineTwo(addressLineTwo.getValue());
+ this.getModelObject().getAddress().setCity(addressCity.getValue());
+ this.getModelObject().getAddress().setRegion(addressRegion.getValue());
+ this.getModelObject().getAddress().setCountry(addressCountry.getValue());
+ this.getModelObject().getAddress().setPostalCode(addressPostalCode.getValue());
+
+ super.onValidate(); //To change body of overridden methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected void onSubmit() {
+
+ info("No Errors");
+ }
+ };
formValidator.add(BeanFormValidator.create(Person.class));
- formValidator.add(new FeedbackPanel("feedback2", new ContainerFeedbackMessageFilter(formValidator)),
- new TextField<String>("name2"),
- new TextField<String>("address.lineOne2"),
- new TextField<String>("address.lineTwo2"),
- new TextField<String>("address.city2"),
- new TextField<String>("address.region2"),
- new TextField<String>("address.country2"),
- new TextField<String>("address.postalCode2"),
- new Button("submit2") {
- @Override
- public void onSubmit() {
- super.onSubmit();
- info("No Errors");
- }
- });
+ formValidator.add(new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(formValidator)));
+ formValidator.add(
+ name,
+ addressLineOne,
+ addressLineTwo,
+ addressCity,
+ addressRegion,
+ addressCountry,
+ addressPostalCode,
+ new Button("submit"));
this.add(formValidator);
View
4 demo/src/main/resources/log4j.properties
@@ -10,4 +10,6 @@ log4j.logger.org.apache.wicket.version=INFO
log4j.logger.org.apache.wicket.RequestCycle=INFO
-log4j.logger.wicket.validation=TRACE
+log4j.logger.wicket.validation.PropertyModelValidationListener=INFO
+log4j.logger.wicket.validation.BeanFormValidator=TRACE
+log4j.logger.wicket.validation.BeanValidator=INFO

0 comments on commit 1c6d969

Please sign in to comment.
Something went wrong with that request. Please try again.