InputElement"

From Documentation
Line 13: Line 13:
  
 
You sholuld not deirectly use this class, please use the inherited class.
 
You sholuld not deirectly use this class, please use the inherited class.
 +
 +
= Validation =
 +
 +
There are two ways to validate the value entered by an user: implementing <javadoc>org.zkoss.zul.Constraint</javadoc> or throwing <javadoc>org.zkoss.zk.ui.WrongValueException</javadoc>.
 +
 +
== Constraint ==
 +
 +
An input element can be associated with a constraint (<javadoc>org.zkoss.zul.Constraint</javadoc>) to validate the value entered by an user. There is a default implementation called <javadoc>org.zkoss.zul.SimpleConstraint</javadoc> that can handle many conditions. If it is not enough, you can implement your own constraint, or throwing WrongValueException as described in the next section.
 +
 +
<source lang="java">
 +
public class EventNumberConstraint implements Constraint {
 +
public void validate(Component comp, Object value)
 +
throws WrongValueException {
 +
if (value != null && (value.intValue() & 1) != 0) //assume used with intbox
 +
throw new WrongValueException(comp, "Only even numbers are allowed, not "+value);
 +
}
 +
}
 +
</source>
 +
 +
== WrongValueException ==
 +
 +
In additions to throwing WrongValueException in <javadoc method="validate(org.zkoss.zk.ui.Component, java.lang.Object)">org.zkoss.zul.Constraint</javadoc>, you can throw WrongValueException in other situation. For example, you can validate the user name and password when the user presses the login button. For example,
 +
 +
<source lang="java">
 +
public class FooComposer extends GenericForwardComposer {
 +
  private Textbox username;
 +
  private Textbox password;
 +
 +
  public void onClick$login() {
 +
    username.clearErrorMessage();
 +
    if (examine(username, password)) {
 +
      //success
 +
    } else {
 +
      throw new WrongValueException(username, "Not a valid username or password. Please retry.");
 +
    }
 +
  }
 +
}
 +
</source>
 +
 +
However, notice that you have to clear the error message manually by invoking <javadoc method="clearErrorMessage()">org.zkoss.zul.impl.InputElement</javadoc>. Otherwise, the error message will remain there unless <javadoc method="setValue(java.lang.String)">org.zkoss.zul.Textbox</javadoc> is called.
  
 
=Example=
 
=Example=

Revision as of 07:09, 13 August 2010

Input Element

Employment/Purpose

InputElement is a super class for components which provie user key input, such as Textbox, Intbox, etc.

Some features are implemented in this class, such as constraint, disabled, maxlength, name, readonly, etc.

You sholuld not deirectly use this class, please use the inherited class.

Validation

There are two ways to validate the value entered by an user: implementing Constraint or throwing WrongValueException.

Constraint

An input element can be associated with a constraint (Constraint) to validate the value entered by an user. There is a default implementation called SimpleConstraint that can handle many conditions. If it is not enough, you can implement your own constraint, or throwing WrongValueException as described in the next section.

public class EventNumberConstraint implements Constraint {
	public void validate(Component comp, Object value)
	throws WrongValueException {
		if (value != null && (value.intValue() & 1) != 0) //assume used with intbox
			throw new WrongValueException(comp, "Only even numbers are allowed, not "+value);
	}
}

WrongValueException

In additions to throwing WrongValueException in Constraint.validate(Component, Object), you can throw WrongValueException in other situation. For example, you can validate the user name and password when the user presses the login button. For example,

public class FooComposer extends GenericForwardComposer {
  private Textbox username;
  private Textbox password;

  public void onClick$login() {
    username.clearErrorMessage();
    if (examine(username, password)) {
      //success
    } else {
      throw new WrongValueException(username, "Not a valid username or password. Please retry.");
    }
  }
}

However, notice that you have to clear the error message manually by invoking InputElement.clearErrorMessage(). Otherwise, the error message will remain there unless Textbox.setValue(String) is called.

Example

N/A

Supported events

Name
Event Type
onOK
'Event:' KeyEvent

[#DropEvent Denotes user has pressed the ][#DropEvent ENTER][#DropEvent key.]

Supported Children

*None

Use cases

Version Description Example Location
     

Version History

Version Date Content
     



Last Update : 2010/08/13

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