Validation"

From Documentation
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ZKDevelopersReferencePageHeader}}
+
#REDIRECT [[ZK Developer's Reference/MVVM/Data Binding]]
 
 
= Validate Each Component at onBindingSave =
 
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.
 
 
 
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 (self.value < 18)
 
      throw new WrongValueException("Age below 18 is not allowed to enter this site.")
 
</attribute>
 
</intbox>
 
</source>
 
 
 
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 at onBindingValidate =
 
Then, after posting <tt>onBindingSave</tt> events to all of binding components to make sure their values are validated. Then, data binding will post an <tt>onBindingValidate</tt> event to the ZK component which triggers data binding to work, for example, a button.
 
 
 
<source lang="xml">
 
<button id="btn" label="submit">
 
  <custome-attributes passed="false"/>
 
  <attribute name="onBindingValidate">
 
    self.setAttribute("passed",true);
 
  </attribute>
 
  <attribute name="onClick">
 
    if (self.getAttribute("passed") == true)
 
        Executions.sendRedirect("index.zul");
 
  </attribute>
 
</button>
 
</source>
 
 
 
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
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
 
 
{{ZKDevelopersReferencePageFooter}}
 

Latest revision as of 03:53, 10 February 2012