Back to top
 
 
 

Customizing Validation Rules

You can customize validation rules for predefined properties in a model or apply validation rules on custom fields for an existing model.

Configuring validation Rules or Constraints

CloudPortal Business Manager ships a bundle citrix.cpbm.custom.common which contains a file named validation-constraints.xml (path: citrix.cpbm.custom.common/src/main/resources/validation-constraints.xml). Any new validation constraints should be added or modified in this file. The file name should not be changed as CloudPortal Business Manager looks for this file in this bundle to bootstrap the validation constraints. As part of implementing validations, Hibernate Validator is used which is an implementation of JSR-303 specification.

Sample validation-constraint.xml
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"        xmlns="http://jboss.org/xml/ns/javax/validation/mapping">            <bean class="com.citrix.cpbm.access.Tenant">            <getter name="owner">                <valid />            </getter>            <getter name="address">               <valid />           </getter>       </bean>       <bean class="com.citrix.cpbm.access.User">           <getter name="firstName">               <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                   <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                   </message>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                  <message>javax.validation.constraints.Size.message</message>                   <element name="min">1</element>                   <element name="max">255</element>               </constraint>           </getter>           <getter name="lastName">               <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                   <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                   </message>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                   <message>javax.validation.constraints.Size.message</message>                   <element name="min">1</element>                   <element name="max">255</element>               </constraint>           </getter>           <getter name="email">               <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                   <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                   </message>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Email">                   <message>javax.validation.constraints.Email.message</message>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                   <message>javax.validation.constraints.Size.message</message>                   <element name="min">5</element>                   <element name="max">255</element>               </constraint>           </getter>           <getter name="username">               <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                   <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                   </message>               </constraint>               <constraint annotation="javax.validation.constraints.Pattern">                   <message>javax.validation.constraints.Pattern.message</message>                   <element name="regexp">[a-zA-Z0-9_@\-\.]+</element>               </constraint>               <constraint annotation="javax.validation.constraints.Size">                   <message>javax.validation.constraints.MinimumSize.message</message>                   <element name="min">5</element>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                   <message>javax.validation.constraints.Size.message</message>                   <element name="min">5</element>                   <element name="max">255</element>               </constraint>           </getter>           <getter name="phone">               <constraint annotation="javax.validation.constraints.Pattern">                   <message>javax.validation.constraints.Pattern.message</message>                   <element name="regexp">(d+)(-)(d+)</element>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                   <message>javax.validation.constraints.Size.message</message>                   <element name="min">1</element>                   <element name="max">20</element>               </constraint>           </getter>       </bean>       <bean class="com.vmops.model.Address">           <getter name="street1">               <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                   <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                   </message>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                   <message>javax.validation.constraints.Size.message</message>                   <element name="min">1</element>                   <element name="max">255</element>               </constraint>           </getter>           <getter name="city">               <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                   <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                   </message>               </constraint>               <constraint annotation="org.hibernate.validator.constraints.Length">                   <message>javax.validation.constraints.Size.message</message>                 <element name="min">1</element>                  <element name="max">255</element>              </constraint>          </getter>          <getter name="state">              <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                  <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                  </message>              </constraint>              <constraint annotation="org.hibernate.validator.constraints.Length">                  <message>javax.validation.constraints.Size.message</message>                 <element name="min">1</element>                  <element name="max">255</element>              </constraint>         </getter>          <getter name="postalCode">              <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                  <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                  </message>             </constraint>              <constraint annotation="org.hibernate.validator.constraints.Length">                  <message>javax.validation.constraints.Size.message</message>                  <element name="min">1</element>                  <element name="max">255</element>              </constraint>          </getter>          <getter name="country">              <constraint annotation="com.citrix.cpbm.validator.constraint.NotBlank">                 <message>com.citrix.cpbm.validator.constraint.impl.NotBlank.message                  </message>              </constraint>             <constraint annotation="org.hibernate.validator.constraints.Length">                  <message>javax.validation.constraints.Size.message</message>                  <element name="min">1</element>                  <element name="max">255</element>              </constraint>         </getter>      </bean>      <bean class="com.citrix.cpbm.access.Subscription">       </bean>    </constraint-mappings>  
Built-in Constraints
Annotation Part of Bean Validation Specification Apply On Use Metadata impact
javax.validation.constraints.AssertFalse yes field/property check that the annotated element is false. none
javax.validation.constraints.AssertTrue yes field/property check that the annotated element is true. none
javax.validation.constraints.DecimalMax yes field/property. Supported types are BigDecimal, BigInteger, String, byte, short, int, long and the respective wrappers of the primitive types. The annotated element must be a number whose value must be lower or equal to the specified maximum. The parameter value is the string representation of the max value according to the BigDecimal string representation. none
javax.validation.constraints.DecimalMin yes field/property. Supported types are BigDecimal, BigInteger, String, byte, short, int, long and the respective wrappers of the primitive types. The annotated element must be a number whose value must be higher or equal to the specified minimum. The parameter value is the string representation of the min value according to the BigDecimal string representation. none
javax.validation.constraints.Digits(integer=, fraction=) yes field/property. Supported types are BigDecimal, BigInteger, String, byte, short, int, long and the respective wrappers of the primitive types. Check whether the property is a number having up to integer digits and fraction fractional digits. Define column precision and scale.
org.hibernate.validator.constraints.Email no field/property. Needs to be a string. Check whether the specified string is a valid email address. none
javax.validation.constraints.Future yes field/property. Supported types are java.util.Date and java.util.Calendar. Checks whether the annotated date is in the future. none
org.hibernate.validator.constraints.Length(min=, max=) no field/property. Needs to be a string. Validate that the annotated string is between min and max included. none
javax.validation.constraints.Max yes field/property. Supported types are BigDecimal, BigInteger, String, byte, short, int, long and the respective wrappers of the primitive types. Checks whether the annotated value is less than or equal to the specified maximum. Add a check constraint on the column.
javax.validation.constraints.Min yes field/property. Supported types are BigDecimal, BigInteger, String, byte, short, int, long and the respective wrappers of the primitive types. Check whether the annotated value is higher than or equal to the specified minimum. Add a check constraint on the column.
javax.validation.constraints.NotNull yes field/property. Check that the annotated value is not null. Column(s) are not null.
org.hibernate.validator.constraints.NotEmpty no field/property. Needs to be a string. Check if the string is not null nor empty. none
javax.validation.constraints.Null yes field/property Check that the annotated value is null. none
javax.validation.constraints.Past yes field/property. Supported types are java.util.Date and java.util.Calendar. Checks whether the annotated date is in the past. none
javax.validation.constraints.Pattern(regex=, flag=) yes field/property. Needs to be a string. Check if the annotated string match the regular expression regex. none
org.hibernate.validator.constraints.Range(min=, max=) no field/property. Supported types are BigDecimal, BigInteger, String, byte, short, int, long and the respective wrappers of the primitive types Check whether the annotated value lies between (inclusive) the specified minimum and maximum. none
javax.validation.constraints.Size(min=, max=) yes field/property. Supported types are String, Collection, Map and arrays. Check if the annotated element size is between min and max (inclusive). Column length will be set to max.
javax.validation.constraints.Valid yes field/property . Perform validation recursively on the associated object. none
Advanced CPBM Defined Constraints
Annotation Part of Bean Validation Specification Apply On Use Metadata impact
com.citrix.cpbm.validator.constraint.CreditCard no field/property check that the annotated element is a valid credit card. none
com.citrix.cpbm.validator.constraint.DateRange no field/property check that the annotated element is in between the specified range. none
com.citrix.cpbm.validator.constraint.IPAddress no field/property check that the annotated element is a valid IpV4/IpV6. none
com.citrix.cpbm.validator.constraint.List no field/property check that the annotated element is one of the element defined in the list. none
com.citrix.cpbm.validator.constraint.NotBlank no field/property check that the annotated element is not empty or blank or null. none
com.citrix.cpbm.validator.constraint.Url no field/property check that the annotated element is valid URL. none
com.citrix.cpbm.validator.constraint.ValueOrRange no field/property check that the annotated element is either equal to given value or in the given range none

Examples- Advanced Constraints

Date Range
<constraint annotation="com.citrix.cpbm.validator.constraint.DateRange" >     <element name="startDate">2013-10-01</element>     <element name="endDate">2013-12-01</element>     <element name="format">yyyy-MM-dd</element>  </constraint>
List
<constraint annotation="com.citrix.cpbm.validator.constraint.List">      <element name="value">       <value>INDIA</value>       <value>USA</value>      </element>  </constraint>  
<constraint annotation="com.citrix.cpbm.validator.constraint.ValueOrRange">      <!-- returns true if target value matches the value or target value is in range of min - max-->      <message>js.errors.accounttype.maxuser.valueerror</message>      <element name="value">-1</element>      <element name="min">1</element>      <element name="max">99999999</element>  </constraint>  

Resolving Message Codes

Whenever a constraint violation happens, it is likely that we would like to support the same via customized error message. Hence, in order to achieve the same, we can add message codes to src/main/resources/OSGI-INF/l10n/resources/CustomApplicationResources.properties and corresponding error messages.

Now in case of constraint violation, error message codes would be resolved with proper error message and gets its passed to subsequent layers i.e. API/JSP/Controllers.
<constraint annotation="com.citrix.cpbm.validator.constraint.List">     <element name="message">list.validator.constraint.error.message.code</element>      <element name="value">      <value>INDIA</value>       <value>USA</value>      </element>  </constraint>  
 

Comments