Validation"

From Documentation
m
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ZKDevelopersReferencePageHeader}}
+
#REDIRECT [[ZK Developer's Reference/MVVM/Data Binding]]
 
 
In additions 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.
 
 
 
= 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 do the validation with your customized way by registering <tt>onBindingSave</tt> 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.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 use of <javadoc method="getValues()">org.zkoss.zkplus.databind.BindingValidateEvent</javadoc>. A typical use is to start [http://people.redhat.com/~ebernard/validation/ JSR 303 validation].
 
 
 
Finally, after posting all events of validation phase to all related ZK components, data binding will truly save data into data bean.
 
 
 
=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