You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2012/03/20 12:07:04 UTC
git commit: WICKET-3879 Support FormValidator and package level
resource bundles
Updated Branches:
refs/heads/master 02c18c942 -> 843fcf628
WICKET-3879 Support FormValidator and package level resource bundles
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/843fcf62
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/843fcf62
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/843fcf62
Branch: refs/heads/master
Commit: 843fcf6287534f8ee481d2e4da63bfd52d76f2b0
Parents: 02c18c9
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Tue Mar 20 13:06:49 2012 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Tue Mar 20 13:06:49 2012 +0200
----------------------------------------------------------------------
.../loader/ValidatorStringResourceLoader.java | 65 ++++++++++++--
...ringResourceLoaderTest$FormValidator.properties | 1 +
.../loader/ValidatorStringResourceLoaderTest.java | 72 +++++++++++++++
3 files changed, 130 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/843fcf62/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java b/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
index ad7a610..8dc5cbc 100644
--- a/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
+++ b/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java
@@ -19,7 +19,10 @@ package org.apache.wicket.resource.loader;
import java.util.Locale;
import org.apache.wicket.Component;
+import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
+import org.apache.wicket.markup.html.form.validation.FormValidatorAdapter;
+import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidatorAdapter;
import org.slf4j.Logger;
@@ -57,8 +60,10 @@ public class ValidatorStringResourceLoader extends ComponentStringResourceLoader
public String loadStringResource(Class<?> clazz, final String key, final Locale locale,
final String style, final String variation)
{
- // only care about IValidator subclasses
- if (clazz == null || !IValidator.class.isAssignableFrom(clazz))
+ // only care about IValidator/IFormValidator subclasses
+ if (
+ clazz == null ||
+ !(IValidator.class.isAssignableFrom(clazz) || IFormValidator.class.isAssignableFrom(clazz)))
{
return null;
}
@@ -74,24 +79,54 @@ public class ValidatorStringResourceLoader extends ComponentStringResourceLoader
public String loadStringResource(final Component component, final String key,
final Locale locale, final String style, final String variation)
{
- if (component == null || !(component instanceof FormComponent))
+
+ final String resource;
+ if (component instanceof FormComponent)
{
- return null;
+ resource = loadStringResource((FormComponent) component, key, locale, style, variation);
+ }
+ else if (component instanceof Form)
+ {
+ resource = loadStringResource((Form) component, key, locale, style, variation);
+ }
+ else
+ {
+ resource = null;
}
- FormComponent<?> fc = (FormComponent<?>)component;
- for (IValidator<?> validator : fc.getValidators())
+ return resource;
+ }
+
+
+ private String loadStringResource(Form<?> form, final String key,
+ final Locale locale, final String style, final String variation)
+ {
+ for (IFormValidator validator : form.getFormValidators())
{
Class<?> scope = getScope(validator);
String resource = loadStringResource(scope, key, locale, style,
- variation);
+ variation);
if (resource != null)
{
return resource;
}
}
+ return null;
+ }
- // not found
+ private String loadStringResource(FormComponent<?> fc, final String key,
+ final Locale locale, final String style, final String variation)
+ {
+ for (IValidator<?> validator : fc.getValidators())
+ {
+ Class<?> scope = getScope(validator);
+ String resource = loadStringResource(scope, key, locale, style,
+ variation);
+ if (resource != null)
+ {
+ return resource;
+ }
+ }
return null;
}
@@ -108,4 +143,18 @@ public class ValidatorStringResourceLoader extends ComponentStringResourceLoader
}
return scope;
}
+
+ private Class<? extends IFormValidator> getScope(IFormValidator formValidator)
+ {
+ Class<? extends IFormValidator> scope;
+ if (formValidator instanceof FormValidatorAdapter)
+ {
+ scope = ((FormValidatorAdapter) formValidator).getValidator().getClass();
+ }
+ else
+ {
+ scope = formValidator.getClass();
+ }
+ return scope;
+ }
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/843fcf62/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$FormValidator.properties
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$FormValidator.properties b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$FormValidator.properties
new file mode 100644
index 0000000..114ae8d
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest$FormValidator.properties
@@ -0,0 +1 @@
+formValidatorFailed=Form Validator loaded OK
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/wicket/blob/843fcf62/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
index bf6757f..f19863f 100644
--- a/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoaderTest.java
@@ -21,8 +21,13 @@ import org.apache.wicket.WicketTestCase;
import org.apache.wicket.markup.IMarkupResourceStreamProvider;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.markup.html.form.validation.IFormValidator;
+import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
+import org.apache.wicket.util.lang.Objects;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.tester.FormTester;
@@ -73,6 +78,17 @@ public class ValidatorStringResourceLoaderTest extends WicketTestCase
tester.assertErrorMessages("Class error loaded OK");
}
+ @Test
+ public void formValidator()
+ {
+ tester.startPage(new FormValidatorPage());
+ FormTester formTester = tester.newFormTester("form");
+ formTester.setValue("field1", "value1");
+ formTester.setValue("field2", "value2");
+ formTester.submit();
+ tester.assertErrorMessages("Form Validator loaded OK");
+ }
+
private static class ValidatorLoaderPage extends WebPage implements IMarkupResourceStreamProvider
{
private ValidatorLoaderPage(IValidator<String> validator)
@@ -112,4 +128,60 @@ public class ValidatorStringResourceLoaderTest extends WicketTestCase
validatable.error(error);
}
}
+
+ private static class FormValidatorPage extends WebPage implements IMarkupResourceStreamProvider
+ {
+ private FormValidatorPage()
+ {
+ FormValidatorEntity entity = new FormValidatorEntity();
+ CompoundPropertyModel<FormValidatorEntity> model = new CompoundPropertyModel<FormValidatorEntity>(entity);
+ Form<FormValidatorEntity> form = new Form<FormValidatorEntity>("form", model);
+ add(form);
+
+ TextField<String> field1 = new TextField<String>("field1");
+ TextField<String> field2 = new TextField<String>("field2");
+ form.add(field1, field2);
+
+ form.add(new FormValidator(field1, field2));
+ }
+
+ @Override
+ public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass)
+ {
+ return new StringResourceStream("<html><body><form wicket:id='form'><input wicket:id='field1'/><input wicket:id='field2'/></form></body></html>");
+ }
+ }
+
+ private static class FormValidator implements IFormValidator
+ {
+ private final FormComponent<?> fc1;
+ private final FormComponent<?> fc2;
+
+ private FormValidator(FormComponent<?> fc1, FormComponent<?> fc2)
+ {
+ this.fc1 = fc1;
+ this.fc2 = fc2;
+ }
+
+ @Override
+ public FormComponent<?>[] getDependentFormComponents()
+ {
+ return new FormComponent<?>[] {fc1, fc2};
+ }
+
+ @Override
+ public void validate(Form<?> form)
+ {
+ if (Objects.equal(fc1.getRawInput(), fc2.getRawInput()) == false)
+ {
+ form.error(form.getString("formValidatorFailed"));
+ }
+ }
+ }
+
+ private static class FormValidatorEntity
+ {
+ private String field1;
+ private String field2;
+ }
}