Validation"

From Documentation
Line 17: Line 17:
 
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.
 
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 at onBindingValidate =
+
= Validate All Beans 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.  
+
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">
 
<source lang="xml">
 
<button id="btn" label="submit">
 
<button id="btn" label="submit">
  <custome-attributes passed="false"/>
 
 
   <attribute name="onBindingValidate">
 
   <attribute name="onBindingValidate">
     self.setAttribute("passed",true);
+
     if (myValidator.validate(event.getReferences(), event.getValues()))
 +
        self.setAttribute("passed",true);
 
   </attribute>
 
   </attribute>
 
   <attribute name="onClick">
 
   <attribute name="onClick">
 
     if (self.getAttribute("passed") == true)
 
     if (self.getAttribute("passed") == true)
        Executions.sendRedirect("index.zul");
+
        Executions.sendRedirect("index.zul");
 
   </attribute>
 
   </attribute>
 
</button>
 
</button>
 
</source>
 
</source>
  
Finally, after posting all events of validation phase to all related ZK components, data binding will truly save data into data bean.  
+
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=
 
=Version History=

Revision as of 06:42, 9 May 2011

Validate Each Bean at onBindingSave

In additions to InputElement.setConstraint(String), Data Binding Manager provides an alternative way to do the validation.

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 (myValidator.validate(event.getReferences(), event.getValues()))
         self.setAttribute("passed",true);
   </attribute>
  <attribute name="onClick">
     if (self.getAttribute("passed") == true)
         Executions.sendRedirect("index.zul");
  </attribute>
</button>

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(). A typical use is to start 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

Last Update : 2011/05/09


Version Date Content
     



Last Update : 2011/05/09

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