Validation"

From Documentation
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
= Validate Each Component at onBindingSave =
+
= Validate Each Bean 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.
 
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.
  
Line 9: Line 9:
 
<intbox value="@{person.age, save-when='btn.onClick'}">
 
<intbox value="@{person.age, save-when='btn.onClick'}">
 
<attribute name="onBindingSave">
 
<attribute name="onBindingSave">
   if (self.value < 18)
+
   if (event.value < 18)
 
       throw new WrongValueException("Age below 18 is not allowed to enter this site.")
 
       throw new WrongValueException("Age below 18 is not allowed to enter this site.")
 
</attribute>
 
</attribute>
Line 15: Line 15:
 
</source>
 
</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.
+
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 at onBindingValidate =

Revision as of 06:37, 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 at onBindingValidate

Then, after posting onBindingSave events to all of binding components to make sure their values are validated. Then, data binding will post an onBindingValidate event to the ZK component which triggers data binding to work, for example, a button.

<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>

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.