Validation"

From Documentation
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
In addition to <javadoc method="setConstraint(java.lang.String)">org.zkoss.zul.impl.InputElement</javadoc>, Data Binding Manager provides an alternative way to do the validation. Of course, it is optional and depends on the requirement of your application.
+
In addition to <javadoc method="setConstraint(java.lang.String)">org.zkoss.zul.impl.InputElement</javadoc>, the Data Binding Manager provides an alternative way to do the validation. Of course, this is optional and depends on the requirement of your application.
  
 
= Validate Each Bean at onBindingSave =
 
= Validate Each Bean at onBindingSave =

Revision as of 02:30, 25 July 2011

In addition to InputElement.setConstraint(String), the Data Binding Manager provides an alternative way to do the validation. Of course, this is optional and depends on the requirement of your application.

Validate Each Bean at onBindingSave

Before saving a value into a bean, the onBindingSave event is sent to each of binding components, so that you could do the validation with your customized way by registering onBindingSave event listener in these components. For example,

<intbox value="@{person.age, save-when='btn.onClick'}">
<attribute name="onBindingSave">
   if (event.value < 18)
      throw new WrongValueException("Age below 18 is not allowed to enter this site.")
</attribute>
</intbox>

As shown above, the onBindingSave event is sent with an instance of BindingSaveEvent, and you could retrieve the detailed information from it.

Validate All Beans at onBindingValidate

After sending the onBindingSave events to all of binding components to make sure their values are validated, the Data Binding Manager will send an onBindingValidate event to the component which triggers data binding to work, for example, a button. For example,

<button id="btn" label="submit">
  <attribute name="onBindingValidate">
     if (jsr303.validate(event.getReferences(), event.getBindings(), event.getValues()))
         self.setAttribute("passed",true);
   </attribute>
  <attribute name="onClick">
     if (self.getAttribute("passed") == true)
         Executions.sendRedirect("index.zul");
  </attribute>
</button>

As shown above, the onBindingValidate event is sent with an instance of BindingValidateEvent, and you could retrieve the detailed information from it. For example, the values could be retrieved by use of BindingValidateEvent.getValues(), and the binding information (Binding) can be retrieved by use of BindingValidateEvent.getBindings(). A typical use is to start JSR 303 validation.

Furthermore, the bean can be retrieved by use of Binding.getBean(Component), and the property's name can be retrieved by use of Binding.getExpression(), such as

String expr = binding.getExpression(); //format: name1.name2.name3
String property = expr.substring(expr.lastIndexOf('.') + 1); //last part is the property's name

Finally, after posting all events of validation phase to all related ZK components, data binding will truly save data into data bean.

For a complete example, please refer to Integrate ZK with JSR 303: Bean Validation.

Version History

Last Update : 2011/07/25


Version Date Content
5.0.7 May 2011 BindingValidateEvent was introduced to simplify the custom validation.



Last Update : 2011/07/25

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.