You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ad...@apache.org on 2014/09/11 13:15:39 UTC
git commit: Added custom constraint to bean validation example
Conflicts:
wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java
Repository: wicket
Updated Branches:
refs/heads/wicket-6.x e954a5e18 -> 72dd4d205
Added custom constraint to bean validation example
Conflicts:
wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/72dd4d20
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/72dd4d20
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/72dd4d20
Branch: refs/heads/wicket-6.x
Commit: 72dd4d20555adf7b8ee7f995cf638974c9811d4c
Parents: e954a5e
Author: Andrea Del Bene <an...@gmail.com>
Authored: Thu Sep 11 11:32:31 2014 +0200
Committer: adelbene <an...@gmail.com>
Committed: Thu Sep 11 13:10:23 2014 +0200
----------------------------------------------------------------------
.../bean/validation/BeanValidationPage.html | 6 ++
.../bean/validation/BeanValidationPage.java | 3 +-
.../validation/BeanValidationPage.properties | 2 +
.../wicket/examples/bean/validation/Person.java | 12 ++++
.../validation/constraint/ValidPassword.java | 42 ++++++++++++++
.../constraint/ValidPasswordValidator.java | 58 ++++++++++++++++++++
.../wicket/examples/homepage/HomePage.html | 2 +-
7 files changed, 123 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.html
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.html
index f266cb9..4ac3cb5 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.html
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.html
@@ -35,6 +35,12 @@
<td><pre class="note">m/d/yyyy field with @Past</pre></td>
</tr>
<tr>
+ <td><label wicket:for="password"><wicket:label>Password</wicket:label></label></td>
+ <td><input wicket:id="password" type="text" size="10"/></td>
+ <td><pre class="note">Custom constraint @ValidPassword with custom message bundles.<br/>A valid password must contain only alphanumeric chars and at least two digits.</pre>
+ </td>
+ </tr>
+ <tr>
<td></td>
<td>
<input type="submit" value="Submit"/>
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java
index 8560738..c8dd758 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.java
@@ -44,6 +44,7 @@ public class BeanValidationPage extends WicketExamplePage
form.add(new TextField<String>("email", new PropertyModel<String>(this, "person.email")).add(new PropertyValidator<String>()));
form.add(new DateTextField("birthdate", new PropertyModel<Date>(this, "person.birthdate"),
new StyleDateConverter("S-", true)).add(new PropertyValidator<String>()));
+ form.add(new TextField<String>("password", new PropertyModel<String>(this, "person.password")).add(new PropertyValidator<String>()));
}
-}
\ No newline at end of file
+}
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.properties
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.properties b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.properties
index c89b963..4d499b1 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.properties
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/BeanValidationPage.properties
@@ -12,3 +12,5 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+password.needDigits=You need to have at least 2 digits in your password.
+password.validChars=Password value can contain only characters and digits.
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/Person.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/Person.java b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/Person.java
index 286627a..59293dc 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/Person.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/Person.java
@@ -24,6 +24,7 @@ import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
+import org.apache.wicket.examples.bean.validation.constraint.ValidPassword;
import org.hibernate.validator.constraints.Email;
public class Person implements Serializable
@@ -41,6 +42,9 @@ public class Person implements Serializable
@Past
private Date birthdate;
+
+ @ValidPassword
+ private String password;
public String getName()
{
@@ -82,5 +86,13 @@ public class Person implements Serializable
this.birthdate = birthdate;
}
+ public String getPassword()
+ {
+ return password;
+ }
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPassword.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPassword.java b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPassword.java
new file mode 100644
index 0000000..2ea66ff
--- /dev/null
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPassword.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.examples.bean.validation.constraint;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+@Target( { METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = ValidPasswordValidator.class)
+@Documented
+public @interface ValidPassword
+{
+ String message() default "{password.validChars}";
+
+ Class<?>[] groups() default {};
+
+ Class<? extends Payload>[] payload() default {};
+}
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPasswordValidator.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPasswordValidator.java b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPasswordValidator.java
new file mode 100644
index 0000000..11802e6
--- /dev/null
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/bean/validation/constraint/ValidPasswordValidator.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.examples.bean.validation.constraint;
+
+import java.util.regex.Pattern;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+
+public class ValidPasswordValidator implements ConstraintValidator<ValidPassword, String>
+{
+
+ private final Pattern content = Pattern.compile("[0-9a-zA-Z]*");
+ private final Pattern digits = Pattern.compile("(.*\\d.*){2}");
+
+ @Override
+ public void initialize(ValidPassword constraintAnnotation)
+ {
+
+ }
+
+ @Override
+ public boolean isValid(String value, ConstraintValidatorContext context)
+ {
+ boolean validationResult = true;
+
+ if (!content.matcher(value).matches())
+ {
+ validationResult = false;
+ }
+ else if (!digits.matcher(value).matches())
+ {
+ context.disableDefaultConstraintViolation();
+ context.buildConstraintViolationWithTemplate("{password.needDigits}")
+ .addConstraintViolation();
+
+ validationResult = false;
+ }
+
+ return validationResult;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/wicket/blob/72dd4d20/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html b/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
index e6c5daf..4789751 100644
--- a/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
+++ b/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
@@ -65,7 +65,7 @@
<tr><td align="right"><a href="resourceaggregation">Resource decoration</a></td><td> - Shows aggregation of css and js resources</td></tr>
<tr><td align="right"><a href="atmosphere">Atmosphere</a></td><td> - Atmosphere Framework integration</td></tr>
<tr><td align="right"><a href="cdi">CDI</a></td><td> - Context Dependency and Injection integration</td></tr>
- <tr><td align="right"><a href="bean-validation">Bean Validation</a></td><td> - Bean Validation integration</td></tr>
+ <tr><td align="right"><a href="bean-validation">Bean Validation</a></td><td> - Bean Validation integration (JSR 303)</td></tr>
</tbody>
</table>
</div>