You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2010/02/04 23:10:03 UTC
svn commit: r906681 - in
/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main:
java/org/apache/myfaces/blank/ java/org/apache/myfaces/blank/domain/
java/org/apache/myfaces/blank/validation/
java/org/apache/myfaces/blank/validatio...
Author: gpetracek
Date: Thu Feb 4 22:10:03 2010
New Revision: 906681
URL: http://svn.apache.org/viewvc?rev=906681&view=rev
Log:
hello-bv example
Added:
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraint.java
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraintValidator.java
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Name.java
- copied, changed from r906114, myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Address.java
Removed:
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Address.java
Modified:
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/HelloGroupValidationController.java
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/domain/Person.java
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/helloWorld.jsp
myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/modelValidation01.jsp
Modified: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/HelloGroupValidationController.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/HelloGroupValidationController.java?rev=906681&r1=906680&r2=906681&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/HelloGroupValidationController.java (original)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/HelloGroupValidationController.java Thu Feb 4 22:10:03 2010
@@ -21,7 +21,7 @@
import org.apache.myfaces.blank.domain.Person;
import org.apache.myfaces.blank.validation.group.Admin;
import org.apache.myfaces.blank.validation.group.User;
-import org.apache.myfaces.blank.validation.group.Address;
+import org.apache.myfaces.blank.validation.group.Name;
import org.apache.myfaces.extensions.validator.beanval.annotation.BeanValidation;
import org.apache.myfaces.extensions.validator.beanval.annotation.ModelValidation;
@@ -34,7 +34,7 @@
@BeanValidation(viewIds = "/groupValidation01.jsp", useGroups = User.class),
@BeanValidation(viewIds = "/groupValidation02.jsp", useGroups = Admin.class),
@BeanValidation(viewIds = "/modelValidation01.jsp", useGroups = Admin.class),
- @BeanValidation(viewIds = "/modelValidation01.jsp", useGroups = Address.class,
+ @BeanValidation(viewIds = "/modelValidation01.jsp", useGroups = Name.class,
modelValidation = @ModelValidation(isActive = true))
})
private Person person = new Person();
Modified: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/domain/Person.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/domain/Person.java?rev=906681&r1=906680&r2=906681&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/domain/Person.java (original)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/domain/Person.java Thu Feb 4 22:10:03 2010
@@ -18,14 +18,16 @@
*/
package org.apache.myfaces.blank.domain;
+import org.apache.myfaces.blank.validation.NameConstraint;
import org.apache.myfaces.blank.validation.group.Admin;
import org.apache.myfaces.blank.validation.group.User;
-import org.apache.myfaces.blank.validation.group.Address;
+import org.apache.myfaces.blank.validation.group.Name;
import org.apache.myfaces.extensions.validator.beanval.payload.ViolationSeverity;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
+@NameConstraint(groups = Name.class)
public class Person
{
@NotNull(payload = ViolationSeverity.Warn.class)
@@ -39,13 +41,6 @@
})
private String lastName;
- @NotNull(groups = Address.class, message = "street is required")
- private String street;
- @NotNull(groups = Address.class, message = "zip is required")
- private String zip;
- @NotNull(groups = Address.class, message = "city is required")
- private String city;
-
public String getFirstName()
{
return firstName;
@@ -65,34 +60,4 @@
{
this.lastName = lastName;
}
-
- public String getStreet()
- {
- return street;
- }
-
- public void setStreet(String street)
- {
- this.street = street;
- }
-
- public String getZip()
- {
- return zip;
- }
-
- public void setZip(String zip)
- {
- this.zip = zip;
- }
-
- public String getCity()
- {
- return city;
- }
-
- public void setCity(String city)
- {
- this.city = city;
- }
}
Added: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraint.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraint.java?rev=906681&view=auto
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraint.java (added)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraint.java Thu Feb 4 22:10:03 2010
@@ -0,0 +1,38 @@
+/*
+ * 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.myfaces.blank.validation;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.TYPE;
+
+@Constraint(validatedBy = NameConstraintValidator.class)
+@Target(TYPE)
+@Retention(RUNTIME)
+public @interface NameConstraint
+{
+ String message() default "invalid name";
+
+ Class<?>[] groups() default {};
+
+ Class<? extends Payload>[] payload() default {};
+}
Added: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraintValidator.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraintValidator.java?rev=906681&view=auto
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraintValidator.java (added)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/NameConstraintValidator.java Thu Feb 4 22:10:03 2010
@@ -0,0 +1,47 @@
+/*
+ * 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.myfaces.blank.validation;
+
+import org.apache.myfaces.blank.domain.Person;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class NameConstraintValidator implements
+ ConstraintValidator<NameConstraint, org.apache.myfaces.blank.domain.Person>
+{
+ public void initialize(NameConstraint differentName)
+ {
+ }
+
+ public boolean isValid(Person person, ConstraintValidatorContext constraintValidatorContext)
+ {
+ if(person == null || isEmpty(person.getFirstName()) || isEmpty(person.getLastName()))
+ {
+ return true;
+ }
+
+ return !(person.getFirstName().equals(person.getLastName()));
+ }
+
+ private boolean isEmpty(String value)
+ {
+ return value == null || "".equals(value);
+ }
+}
Copied: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Name.java (from r906114, myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Address.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Name.java?p2=myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Name.java&p1=myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Address.java&r1=906114&r2=906681&rev=906681&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Address.java (original)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/java/org/apache/myfaces/blank/validation/group/Name.java Thu Feb 4 22:10:03 2010
@@ -18,6 +18,6 @@
*/
package org.apache.myfaces.blank.validation.group;
-public interface Address
+public interface Name
{
}
\ No newline at end of file
Modified: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/helloWorld.jsp
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/helloWorld.jsp?rev=906681&r1=906680&r2=906681&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/helloWorld.jsp (original)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/helloWorld.jsp Thu Feb 4 22:10:03 2010
@@ -41,7 +41,7 @@
<hr/>
<h:commandLink value="group validation example 1" action="example01" immediate="true"/><br/>
<h:commandLink value="group validation example 2" action="example02" immediate="true"/><br/>
- <h:commandLink value="model validation example 1 (forced validation error)" action="example03" immediate="true"/><br/>
+ <h:commandLink value="model validation example 1" action="example03" immediate="true"/><br/>
</h:form>
</f:view>
<hr/>
Modified: myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/modelValidation01.jsp
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/modelValidation01.jsp?rev=906681&r1=906680&r2=906681&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/modelValidation01.jsp (original)
+++ myfaces/extensions/validator/trunk/examples/hello_bean-validation/src/main/webapp/modelValidation01.jsp Thu Feb 4 22:10:03 2010
@@ -25,11 +25,11 @@
<f:view>
<h:form id="mainForm">
<h:panelGrid columns="3">
- <h:outputLabel for="first_name" value="First name:"/>
+ <h:outputLabel for="first_name" value="First name*:"/>
<h:inputText id="first_name" label="First name" value="#{helloGroupValidationController['person'].firstName}"/>
<h:message for="first_name" showSummary="true" showDetail="false"/>
- <h:outputLabel for="last_name" value="Last name:"/>
+ <h:outputLabel for="last_name" value="Last name**:"/>
<h:inputText id="last_name" label="Last name" value="#{helloGroupValidationController.person.lastName}"/>
<h:message for="last_name" showSummary="true" showDetail="false"/>
@@ -41,8 +41,7 @@
<h:messages globalOnly="true" showDetail="true" showSummary="false"/>
</f:view>
<hr/>
- it isn't possible to submit this form!<br/>
- reason: model-validation is triggered -> address group is validated
+ model validation: first and last name have to be different<br/>
* not required<br/>
** not required; length of provided value: 3-12 characters