Validation"

From Documentation
 
Line 1: Line 1:
{{ZKDevelopersReferencePageHeader}}
+
#REDIRECT [[ZK Developer's Reference/MVVM/Data Binding]]
 
 
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 =
 
 
 
Before saving a value into a bean, the <tt>onBindingSave</tt> event is sent to each of binding components, so that you could customize the validation by registering <tt>onBindingSave</tt> the event listener in these components. For example,
 
 
 
<source lang="xml">
 
<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>
 
</source>
 
 
 
As shown above, the <tt>onBindingSave</tt> event is sent with an instance of <javadoc>org.zkoss.zkplus.databind.BindingSaveEvent</javadoc>, and you could retrieve the detailed information from it.
 
 
 
= Validate All Beans at onBindingValidate =
 
After sending the <tt>onBindingSave</tt> events to all of binding components to make sure their values are validated, the Data Binding Manager will send an <tt>onBindingValidate</tt> event to the component which triggers data binding to work, for example, a button. For example,
 
 
 
<source lang="xml">
 
<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>
 
</source>
 
 
 
As shown above, the <tt>onBindingValidate</tt> event is sent with an instance of <javadoc>org.zkoss.zkplus.databind.BindingValidateEvent</javadoc>, and you could retrieve the detailed information from it. For example, the values could be retrieved by the use of <javadoc method="getValues()">org.zkoss.zkplus.databind.BindingValidateEvent</javadoc>, and the binding information (<javadoc>org.zkoss.zkplus.databind.Binding</javadoc>) can be retrieved by the use of <javadoc method="getBindings()">org.zkoss.zkplus.databind.BindingValidateEvent</javadoc>. A typical use is to start [http://people.redhat.com/~ebernard/validation/ JSR 303 validation].
 
 
 
Furthermore, the bean can be retrieved by the use of <javadoc method="getBean(org.zkoss.zk.ui.Component)">org.zkoss.zkplus.databind.Binding</javadoc>, and the property's name can be retrieved by the use of <javadoc method="getExpression()">org.zkoss.zkplus.databind.Binding</javadoc>, such as
 
 
 
<source lang="java">
 
String expr = binding.getExpression(); //format: name1.name2.name3
 
String property = expr.substring(expr.lastIndexOf('.') + 1); //the last part is the property's name
 
</source>
 
 
 
Finally, after posting all the 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 [[Small Talks/2011/May/Integrate ZK with JSR 303: Bean Validation|Integrate ZK with JSR 303: Bean Validation]].
 
 
 
=Version History=
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| 5.0.7
 
| May 2011
 
| <javadoc>org.zkoss.zkplus.databind.BindingValidateEvent</javadoc> was introduced to simplify the custom validation.
 
|}
 
 
 
{{ZKDevelopersReferencePageFooter}}
 

Latest revision as of 03:53, 10 February 2012