Validation"

From Documentation
Line 35: Line 35:
  
 
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>, and the binding information (<javadoc>org.zkoss.zkplus.databind.Binding</javadoc>) can be retrieved by 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].
 
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>, and the binding information (<javadoc>org.zkoss.zkplus.databind.Binding</javadoc>) can be retrieved by 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 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 use of <javadoc method="getExpression()">org.zkoss.zkplus.databind.Binding</javadoc>.
  
 
Finally, after posting all events of validation phase to all related ZK components, data binding will truly save data into data bean.
 
Finally, after posting all events of validation phase to all related ZK components, data binding will truly save data into data bean.

Revision as of 09:59, 9 May 2011

In additions to InputElement.setConstraint(String), 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 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 (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>

As shown above, 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(), and the binding information (Binding) can be retrieved by use of BindingValidateEvent.getBindings(). A typical use is to start JSR 303 validation.

Furthermore, the bean can be retrieved by use of Binding.getBean(Component), and the property's name can be retrieved by use of Binding.getExpression().

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
5.0.7 May 2011 BindingValidateEvent was introduced to simplify the custom validation.



Last Update : 2011/05/09

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