JEE

Custom Validator In JSF Page

Found in JavaServerFaces, O'Reilly, Hans Bergsten

Add a Validator to the faces-config.xml

<validator>
	<validator-id>SomeId</validator-id>
	<validator-class>com.yourcompany.YourValidatorClass</validator-class>
</validator>

Add the validator using JSFs generic custom action

<h:inputText id="firstControl" value="#{bean.attribute}">
	<f:validator validatorId="SomeId" />
	<f:attribute name="peerId" value="secondControl" />
</h:inputText>

<h:inputText id="secondControl" ... />

Implement the validator and compare the value to the peer controls value

public class YourValidator implements Validator, Serializable {
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		// Find the peer component.
		final String peerId = (String) component.getAttributes().get("peerId");
		final UIComponent peerComponent = component.findComponent(peerId);
		final ValueHolder peer = (ValueHolder) peerComponent;
		Object value = peer.getValue()
		// Now compare...

Knowledge Base

Game Development

Personal

PmWiki

pmwiki.org

edit SideBar

Page last modified on July 25, 2008, at 08:59 PM