dev@javaserverfaces.java.net

Re: Seeking Review: 1-multiComponentValidation

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Wed, 04 Feb 2009 08:50:49 -0800

Holding off on the review (and by extension allowing the code to be
committed)
until the EG agrees to the proposal.


Ed Burns wrote:
> Yes, this is issue 1 in the Issue tracker. It being so easy to fix,
> what with the new event system and all, that I decided to fix it. Took
> about two hours, most of the time spent on the automated test.
>
> I'm sending this to the EG as well.
>
> Issue: 1
>
> Turns out this was really easy, with the new event system. Thanks very
> much Jason. Most of the time on this was on the automated test.
>
>
> SECTION: Modified Files
> ----------------------------
> A jsf-api/src/javax/faces/event/AfterValidateEvent.java
> A jsf-api/src/javax/faces/event/BeforeValidateEvent.java
>
> - new event types. Documentation pending
>
> M jsf-api/src/javax/faces/component/UIComponentBase.java
>
> - modify the contract of processValidations to publish beforeValidate
> and afterValidate events as appropriate. Note that the #{component}
> implicit object works here.
>
> M jsf-ri/src/com/sun/faces/application/NamedEventManager.java
>
> - Add these to the named event list.
>
> M jsf-ri/systest/src/com/sun/faces/systest/model/EventTagBean.java
>
> - Here's the operative code:
>
> public void afterValidate(ComponentSystemEvent event) {
> FacesContext context = FacesContext.getCurrentInstance();
> final UIForm form = (UIForm) event.getComponent();
> final String [] clientIds = { "lesser", "greater" };
> final int [] values = new int[2];
> final boolean [] hasValues = new boolean[2];
> final List<FacesMessage> toAdd = new ArrayList<FacesMessage>();
>
> // Traverse the form and suck out the individual values
> for (int i = 0; i < clientIds.length; i++) {
> final int finalI = i;
> form.invokeOnComponent(context, clientIds[i], new ContextCallback() {
>
> public void invokeContextCallback(FacesContext context, UIComponent target) {
> Object value = ((ValueHolder) target).getValue();
> try {
> if (null != value) {
> values[finalI] = Integer.parseInt(value.toString());
> hasValues[finalI] = true;
> } else {
> hasValues[finalI] = false;
> FacesMessage msg = new FacesMessage(clientIds[finalI] +
> " must have a value");
> toAdd.add(msg);
> }
> } catch (NumberFormatException nfe) {
> FacesMessage msg = new FacesMessage("unable to parse the number for field " +
> clientIds[finalI]);
> toAdd.add(msg);
> }
>
> }
> });
> }
>
> // case one, ensure both fields have a value
> if (!hasValues[0] || !hasValues[1]) {
> FacesMessage msg = new FacesMessage("both fields must have a value");
> toAdd.add(msg);
> } else {
> // case two, ensure lesser is lesser than greater
> if (!(values[0] < values[1])) {
> FacesMessage msg = new FacesMessage("lesser must be lesser than greater");
> toAdd.add(msg);
> }
> }
>
> // If we have any messages
> if (!toAdd.isEmpty()) {
> // add them so the user sees the message
> String formClientId = form.getClientId(context);
> for (FacesMessage cur : toAdd) {
> context.addMessage(formClientId, cur);
> }
> // skip remaining lifecycle phases
> context.renderResponse();
> }
> }
>
> M jsf-ri/systest/build-tests.xml
> M jsf-ri/systest/build.xml
>
> - Add new testcase
>
> A jsf-ri/systest/web/validator05.xhtml
>
> - xhtml file for new test
>
>
> SECTION: Diffs
> ----------------------------
> Index: jsf-api/src/javax/faces/event/AfterValidateEvent.java
> ===================================================================
> --- jsf-api/src/javax/faces/event/AfterValidateEvent.java (revision 0)
> +++ jsf-api/src/javax/faces/event/AfterValidateEvent.java (revision 0)
> @@ -0,0 +1,35 @@
> +/*
> + * To change this template, choose Tools | Templates
> + * and open the template in the editor.
> + */
> +
> +package javax.faces.event;
> +
> +import javax.faces.component.UIComponent;
> +
> +/**
> + *
> + * <p class="changed_added_2_0"></p>
> + *
> + * @since 2.0
> + */
> +public class AfterValidateEvent extends ComponentSystemEvent {
> +
> +
> + // ------------------------------------------------------------ Constructors
> +
> +
> + /**
> +
> + * <p class="changed_added_2_0"></p>
> +
> + * @param component the <code>UIComponent</code> that is about to be
> + * validated.
> +
> + * @throws <code>IllegalArgumentException</code> if the argument is <code>null</code>.
> + */
> + public AfterValidateEvent(UIComponent component) {
> + super(component);
> + }
> +
> +}
> Index: jsf-api/src/javax/faces/event/BeforeValidateEvent.java
> ===================================================================
> --- jsf-api/src/javax/faces/event/BeforeValidateEvent.java (revision 0)
> +++ jsf-api/src/javax/faces/event/BeforeValidateEvent.java (revision 0)
> @@ -0,0 +1,35 @@
> +/*
> + * To change this template, choose Tools | Templates
> + * and open the template in the editor.
> + */
> +
> +package javax.faces.event;
> +
> +import javax.faces.component.UIComponent;
> +
> +/**
> + *
> + * <p class="changed_added_2_0"></p>
> + *
> + * @since 2.0
> + */
> +public class BeforeValidateEvent extends ComponentSystemEvent {
> +
> +
> + // ------------------------------------------------------------ Constructors
> +
> +
> + /**
> +
> + * <p class="changed_added_2_0"></p>
> +
> + * @param component the <code>UIComponent</code> that is about to be
> + * validated.
> +
> + * @throws <code>IllegalArgumentException</code> if the argument is <code>null</code>.
> + */
> + public BeforeValidateEvent(UIComponent component) {
> + super(component);
> + }
> +
> +}
> Index: jsf-api/src/javax/faces/component/UIComponentBase.java
> ===================================================================
> --- jsf-api/src/javax/faces/component/UIComponentBase.java (revision 6390)
> +++ jsf-api/src/javax/faces/component/UIComponentBase.java (working copy)
> @@ -1076,7 +1076,10 @@
> Iterator kids = getFacetsAndChildren();
> while (kids.hasNext()) {
> UIComponent kid = (UIComponent) kids.next();
> + Application app = context.getApplication();
> + app.publishEvent(BeforeValidateEvent.class, kid);
> kid.processValidators(context);
> + app.publishEvent(AfterValidateEvent.class, kid);
> popComponentFromEL(context);
> }
> }
> Index: jsf-ri/src/com/sun/faces/application/NamedEventManager.java
> ===================================================================
> --- jsf-ri/src/com/sun/faces/application/NamedEventManager.java (revision 6390)
> +++ jsf-ri/src/com/sun/faces/application/NamedEventManager.java (working copy)
> @@ -8,10 +8,11 @@
> import java.util.Map;
> import java.util.Set;
> import java.util.concurrent.ConcurrentHashMap;
> -import javax.faces.context.FacesContext;
> import javax.faces.event.AfterAddToParentEvent;
> import javax.faces.event.AfterAddToViewEvent;
> +import javax.faces.event.AfterValidateEvent;
> import javax.faces.event.BeforeRenderEvent;
> +import javax.faces.event.BeforeValidateEvent;
> import javax.faces.event.SystemEvent;
>
> /**
> @@ -28,9 +29,13 @@
> namedEvents.put("javax.faces.event.beforeRender", BeforeRenderEvent.class);
> namedEvents.put("javax.faces.event.afterAddToParent", AfterAddToParentEvent.class);
> namedEvents.put("javax.faces.event.afterAddToView", AfterAddToViewEvent.class);
> + namedEvents.put("javax.faces.event.BeforeValidate", BeforeValidateEvent.class);
> + namedEvents.put("javax.faces.event.AfterValidate", AfterValidateEvent.class);
> namedEvents.put("beforeRender", BeforeRenderEvent.class);
> namedEvents.put("afterAddToParent", AfterAddToParentEvent.class);
> namedEvents.put("afterAddToView", AfterAddToViewEvent.class);
> + namedEvents.put("beforeValidate", BeforeValidateEvent.class);
> + namedEvents.put("afterValidate", AfterValidateEvent.class);
> }
>
> public void addNamedEvent(String name, Class<? extends SystemEvent> event) {
> Index: jsf-ri/systest/src/com/sun/faces/systest/model/EventTagBean.java
> ===================================================================
> --- jsf-ri/systest/src/com/sun/faces/systest/model/EventTagBean.java (revision 6390)
> +++ jsf-ri/systest/src/com/sun/faces/systest/model/EventTagBean.java (working copy)
> @@ -5,7 +5,15 @@
>
> package com.sun.faces.systest.model;
>
> +import java.util.ArrayList;
> +import java.util.List;
> +import javax.faces.application.FacesMessage;
> +import javax.faces.component.ContextCallback;
> +import javax.faces.component.UIComponent;
> +import javax.faces.component.UIForm;
> import javax.faces.component.UIOutput;
> +import javax.faces.component.ValueHolder;
> +import javax.faces.context.FacesContext;
> import javax.faces.event.ComponentSystemEvent;
>
> /**
> @@ -17,4 +25,64 @@
> UIOutput output = (UIOutput)event.getComponent();
> output.setValue("The '" + event.getClass().getName() + "' event fired!");
> }
> +
> + public void afterValidate(ComponentSystemEvent event) {
> + FacesContext context = FacesContext.getCurrentInstance();
> + final UIForm form = (UIForm) event.getComponent();
> + final String [] clientIds = { "lesser", "greater" };
> + final int [] values = new int[2];
> + final boolean [] hasValues = new boolean[2];
> + final List<FacesMessage> toAdd = new ArrayList<FacesMessage>();
> +
> + // Traverse the form and suck out the individual values
> + for (int i = 0; i < clientIds.length; i++) {
> + final int finalI = i;
> + form.invokeOnComponent(context, clientIds[i], new ContextCallback() {
> +
> + public void invokeContextCallback(FacesContext context, UIComponent target) {
> + Object value = ((ValueHolder) target).getValue();
> + try {
> + if (null != value) {
> + values[finalI] = Integer.parseInt(value.toString());
> + hasValues[finalI] = true;
> + } else {
> + hasValues[finalI] = false;
> + FacesMessage msg = new FacesMessage(clientIds[finalI] +
> + " must have a value");
> + toAdd.add(msg);
> + }
> + } catch (NumberFormatException nfe) {
> + FacesMessage msg = new FacesMessage("unable to parse the number for field " +
> + clientIds[finalI]);
> + toAdd.add(msg);
> + }
> +
> + }
> + });
> + }
> +
> + // case one, ensure both fields have a value
> + if (!hasValues[0] || !hasValues[1]) {
> + FacesMessage msg = new FacesMessage("both fields must have a value");
> + toAdd.add(msg);
> + } else {
> + // case two, ensure lesser is lesser than greater
> + if (!(values[0] < values[1])) {
> + FacesMessage msg = new FacesMessage("lesser must be lesser than greater");
> + toAdd.add(msg);
> + }
> + }
> +
> + // If we have any messages
> + if (!toAdd.isEmpty()) {
> + // add them so the user sees the message
> + String formClientId = form.getClientId(context);
> + for (FacesMessage cur : toAdd) {
> + context.addMessage(formClientId, cur);
> + }
> + // skip remaining lifecycle phases
> + context.renderResponse();
> + }
> + }
> +
> }
> Index: jsf-ri/systest/web/validator05.xhtml
> ===================================================================
> --- jsf-ri/systest/web/validator05.xhtml (revision 0)
> +++ jsf-ri/systest/web/validator05.xhtml (revision 0)
> @@ -0,0 +1,65 @@
> +<?xml version="1.0" encoding="UTF-8"?>
> +<!--
> + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
> +
> + Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
> +
> + The contents of this file are subject to the terms of either the GNU
> + General Public License Version 2 only ("GPL") or the Common Development
> + and Distribution License("CDDL") (collectively, the "License"). You
> + may not use this file except in compliance with the License. You can obtain
> + a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
> + or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
> + language governing permissions and limitations under the License.
> +
> + When distributing the software, include this License Header Notice in each
> + file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
> + Sun designates this particular file as subject to the "Classpath" exception
> + as provided by Sun in the GPL Version 2 section of the License file that
> + accompanied this code. If applicable, add the following below the License
> + Header, with the fields enclosed by brackets [] replaced by your own
> + identifying information: "Portions Copyrighted [year]
> + [name of copyright owner]"
> +
> + Contributor(s):
> +
> + If you wish your version of this file to be governed by only the CDDL or
> + only the GPL Version 2, indicate your decision by adding "[Contributor]
> + elects to include this software in this distribution under the [CDDL or GPL
> + Version 2] license." If you don't indicate a single choice of license, a
> + recipient has the option to distribute your version of this file under
> + either the CDDL, the GPL Version 2 or to extend the choice of license to
> + its licensees as provided above. However, if you add GPL Version 2 code
> + and therefore, elected the GPL Version 2 license, then the option applies
> + only if the new code is made subject to such option by the copyright
> + holder.
> +-->
> +<!DOCTYPE html
> + PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
> + xmlns:h="http://java.sun.com/jsf/html"
> + xmlns:ui="http://java.sun.com/jsf/facelets"
> + xmlns:f="http://java.sun.com/jsf/core">
> +<head>
> + <title>form level validation test</title>
> +</head>
> +<body>
> +<h:form id="form" prependId="false">
> + <f:event type="afterValidate" action="#{eventTagBean.afterValidate}" />
> +
> + <p>lesser: <h:inputText id="lesser" /></p>
> +
> + <p>greater: <h:inputText id="greater" /></p>
> +
> + <p>Verify that the value of "lesser" is lesser than the value of
> + "greater".</p>
> +
> + <p><h:commandButton id="cbutton" value="submit" /></p>
> +
> +</h:form>
> +
> +
> +<h:messages id="messages"/>
> +</body>
> +</html>
> Index: jsf-ri/systest/build-tests.xml
> ===================================================================
> --- jsf-ri/systest/build-tests.xml (revision 6390)
> +++ jsf-ri/systest/build-tests.xml (working copy)
> @@ -770,6 +770,16 @@
> ignoreIfContains="${ignore.path}/ignoreIfContains.txt"
> request="${context.path}/faces/validator.jsp"
> outContent="/validator.jsp PASSED"/>
> +
> + <jsf.junit context-path="${context.path}"
> + classpath-refid="html.classpath"
> + test-results-dir="${impl.test.results.dir}">
> + <tests>
> + <fileset dir="${basedir}/build/classes"
> + includes="com/sun/faces/systest/validator/*TestCase.class"/>
> + </tests>
> + </jsf.junit>
> +
> </target>
>
> <target name="test.validator01"
> Index: jsf-ri/systest/build.xml
> ===================================================================
> --- jsf-ri/systest/build.xml (revision 6390)
> +++ jsf-ri/systest/build.xml (working copy)
> @@ -168,7 +168,7 @@
> <target name="passthru"
> description="useful for running one test">
>
> - <ant antfile="build-tests.xml" target="scratch"/>
> + <ant antfile="build-tests.xml" target="test.validator"/>
>
>
> </target>
>
>
> SECTION: New Files
> ----------------------------
> SEE ATTACHMENTS
>
>
>