Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Testing BeanFormValidator (going to remove it after this commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tilford committed Apr 12, 2010
1 parent ca62b51 commit 1c6d969
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 36 deletions.
12 changes: 8 additions & 4 deletions core/src/main/java/wicket/validation/BeanFormValidator.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* To change this template use File | Settings | File Templates. * To change this template use File | Settings | File Templates.
*/ */
public class BeanFormValidator<T> extends AbstractFormValidator { public class BeanFormValidator<T> extends AbstractFormValidator {
private static final long serialVersionUID = 1L;

private static final Logger logger = LoggerFactory.getLogger(BeanFormValidator.class.getName()); private static final Logger logger = LoggerFactory.getLogger(BeanFormValidator.class.getName());
private final Class<?>[] groups; private final Class<?>[] groups;
private final Class<T> modelType; private final Class<T> modelType;
Expand All @@ -41,10 +43,10 @@ public FormComponent<?>[] getDependentFormComponents() {


@Override @Override
public void validate(Form<?> form) { public void validate(Form<?> form) {
//crazy wicket wildcard generics //crazy wicket wildcard generics ... Form<?> should be Form<T>
if (modelType.isAssignableFrom(form.getModelObject().getClass())) { if (modelType.isAssignableFrom(form.getModelObject().getClass())) {
Set<ConstraintViolation<T>> violations = validateValue((T) form.getModelObject()); Set<ConstraintViolation<Object>> violations = validateValue(form.getModelObject());
for (ConstraintViolation<T> violation : violations) { for (ConstraintViolation<Object> violation : violations) {
form.error(new StringResourceModel(violation.getMessageTemplate(), form, null).getString()); form.error(new StringResourceModel(violation.getMessageTemplate(), form, null).getString());
logger.debug("validation error \"" + violation.getMessageTemplate() + "\""); logger.debug("validation error \"" + violation.getMessageTemplate() + "\"");
} }
Expand All @@ -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); return getValidator().validate(value, groups);
} }


Expand All @@ -71,4 +73,6 @@ protected Validator getValidator() {
public static <T> BeanFormValidator<T> create(final Class<T> modelType, final Class<?>... groups) { public static <T> BeanFormValidator<T> create(final Class<T> modelType, final Class<?>... groups) {
return new BeanFormValidator<T>(modelType, groups); return new BeanFormValidator<T>(modelType, groups);
} }


} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ boolean applyValidator(Component component) {
break; break;
} }
} }
logger.debug("Filter result ==" + apply); logger.trace("Filter result ==" + apply);
} }
apply = apply && (formComponent.getModel() instanceof AbstractPropertyModel); apply = apply && (formComponent.getModel() instanceof AbstractPropertyModel);


} }
logger.debug("PropertyModelValidationListener.applyValidator == " + apply); logger.trace("PropertyModelValidationListener.applyValidator == " + apply);
return apply; return apply;
} }
} }
27 changes: 15 additions & 12 deletions demo/src/main/java/wicket/validation/demo/HomePage.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<span wicket:id="message">message will be here</span> <span wicket:id="message">message will be here</span>


<form wicket:id="person"> <form wicket:id="person">
<div wicket:id="feedback"/>



<h1>BeanValidator</h1> <h1>BeanValidator</h1>

<div wicket:id="feedback"/>

<label for="name"> <label for="name">
<wicket:message key="person.name">Name</wicket:message> <wicket:message key="person.name">Name</wicket:message>
</label> </label>
Expand Down Expand Up @@ -55,47 +56,49 @@ <h1>BeanValidator</h1>




<form wicket:id="formValidator"> <form wicket:id="formValidator">
<div wicket:id="feedback2"/>



<h1>BeanFormValidator</h1> <h1>BeanFormValidator</h1>

<div wicket:id="feedback"/>


<label for="name2"> <label for="name2">
<wicket:message key="person.name">Name</wicket:message> <wicket:message key="person.name">Name</wicket:message>
</label> </label>
<input wicket:id="name2" name="name2"/> <input wicket:id="name" name="name2"/>
<br/> <br/>
<label for="address.lineOne2"> <label for="address.lineOne2">
<wicket:message key="address.lineOne">Address</wicket:message> <wicket:message key="address.lineOne">Address</wicket:message>
</label> </label>
<input wicket:id="address.lineOne2" name="address.lineOne2"/> <input wicket:id="address.lineOne" name="address.lineOne2"/>
<input wicket:id="address.lineTwo2" name="address.lineTwo2"/> <input wicket:id="address.lineTwo" name="address.lineTwo2"/>
<br/> <br/>


<label for="address.city2"> <label for="address.city2">
<wicket:message key="address.city">City</wicket:message> <wicket:message key="address.city">City</wicket:message>
</label> </label>
<input wicket:id="address.city2" name="address.city2"/> <input wicket:id="address.city" name="address.city2"/>
<br/> <br/>


<label for="address.region2"> <label for="address.region2">
<wicket:message key="address.region">State</wicket:message> <wicket:message key="address.region">State</wicket:message>
</label> </label>
<input wicket:id="address.region2" name="address.region2"/> <input wicket:id="address.region" name="address.region2"/>
<br/> <br/>


<label for="address.country2"> <label for="address.country2">
<wicket:message key="address.country">Country</wicket:message> <wicket:message key="address.country">Country</wicket:message>
</label> </label>
<input wicket:id="address.country2" name="address.country2"/> <input wicket:id="address.country" name="address.country2"/>
<br/> <br/>


<label for="address.postalCode2"> <label for="address.postalCode2">
<wicket:message key="address.postalCode">Zip Code</wicket:message> <wicket:message key="address.postalCode">Zip Code</wicket:message>
</label> </label>
<input wicket:id="address.postalCode2" name="address.postalCode2"/> <input wicket:id="address.postalCode" name="address.postalCode2"/>
<br/> <br/>


<input wicket:id="submit2" type="submit" value="Submit"/> <input wicket:id="submit" type="submit" value="Submit"/>
</form> </form>


</body> </body>
Expand Down
63 changes: 46 additions & 17 deletions demo/src/main/java/wicket/validation/demo/HomePage.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model; import org.apache.wicket.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wicket.validation.BeanFormValidator; import wicket.validation.BeanFormValidator;
import wicket.validation.demo.beans.Address; import wicket.validation.demo.beans.Address;
import wicket.validation.demo.beans.Person; import wicket.validation.demo.beans.Person;
Expand All @@ -21,6 +23,8 @@ public class HomePage extends WebPage {


private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;


private static final Logger logger = LoggerFactory.getLogger(HomePage.class.getName());

// TODO Add any page properties or variables here // TODO Add any page properties or variables here


/** /**
Expand Down Expand Up @@ -58,26 +62,51 @@ public void onSubmit() {
this.add(personForm); this.add(personForm);




Person person2 = new Person(); final Person person2 = new Person();
person2.setAddress(new Address()); 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(BeanFormValidator.create(Person.class));


formValidator.add(new FeedbackPanel("feedback2", new ContainerFeedbackMessageFilter(formValidator)), formValidator.add(new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(formValidator)));
new TextField<String>("name2"), formValidator.add(
new TextField<String>("address.lineOne2"), name,
new TextField<String>("address.lineTwo2"), addressLineOne,
new TextField<String>("address.city2"), addressLineTwo,
new TextField<String>("address.region2"), addressCity,
new TextField<String>("address.country2"), addressRegion,
new TextField<String>("address.postalCode2"), addressCountry,
new Button("submit2") { addressPostalCode,
@Override new Button("submit"));
public void onSubmit() {
super.onSubmit();
info("No Errors");
}
});


this.add(formValidator); this.add(formValidator);


Expand Down
4 changes: 3 additions & 1 deletion demo/src/main/resources/log4j.properties
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ log4j.logger.org.apache.wicket.version=INFO
log4j.logger.org.apache.wicket.RequestCycle=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.