Care to add a UI test case for it ?
https://github.com/apache/wicket/tree/master/wicket-examples/src/main/webapp/js-test/tests/bean-validation

to run it just open http://localhost:8080/js-test/all.html in a browser

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Sep 11, 2014 at 12:35 PM, <adelb...@apache.org> wrote:

> Repository: wicket
> Updated Branches:
>   refs/heads/master edeb2ccd8 -> 9e03f68f1
>
>
> Added custom constraint to bean validation example
>
> Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
> Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9e03f68f
> Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9e03f68f
> Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9e03f68f
>
> Branch: refs/heads/master
> Commit: 9e03f68f192f6f4e746e2f443ace36e4ff17fc24
> Parents: edeb2cc
> Author: Andrea Del Bene <an.delb...@gmail.com>
> Authored: Thu Sep 11 11:32:31 2014 +0200
> Committer: adelbene <an.delb...@gmail.com>
> Committed: Thu Sep 11 11:34:28 2014 +0200
>
> ----------------------------------------------------------------------
>  .../bean/validation/BeanValidationPage.html     |  6 ++
>  .../bean/validation/BeanValidationPage.java     |  1 +
>  .../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, 122 insertions(+), 1 deletion(-)
> ----------------------------------------------------------------------
>
>
>
> http://git-wip-us.apache.org/repos/asf/wicket/blob/9e03f68f/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/9e03f68f/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 8618b1b..63015df 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<>()));
>                 form.add(new DateTextField("birthdate", new
> PropertyModel<Date>(this, "person.birthdate"),
>                         new StyleDateConverter("S-", true)).add(new
> PropertyValidator<>()));
> +               form.add(new TextField<String>("password", new
> PropertyModel<String>(this, "person.password")).add(new
> PropertyValidator<>()));
>
>         }
>  }
> \ No newline at end of file
>
>
> http://git-wip-us.apache.org/repos/asf/wicket/blob/9e03f68f/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/9e03f68f/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/9e03f68f/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/9e03f68f/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/9e03f68f/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>
>
>

Reply via email to