001// Copyright 2006, 2007, 2008, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.corelib.components;
016
017import org.apache.tapestry5.CSSClassConstants;
018import org.apache.tapestry5.MarkupWriter;
019import org.apache.tapestry5.ValidationTracker;
020import org.apache.tapestry5.annotations.Environmental;
021import org.apache.tapestry5.annotations.Parameter;
022import org.apache.tapestry5.corelib.internal.InternalMessages;
023import org.apache.tapestry5.services.FormSupport;
024
025import java.util.List;
026
027/**
028 * Standard validation error presenter. Must be enclosed by a
029 * {@link org.apache.tapestry5.corelib.components.Form} component. If errors are present, renders a
030 * div element around a banner message and around an unnumbered list of
031 * error messages. Renders nothing if the {@link org.apache.tapestry5.ValidationTracker} shows no
032 * errors.
033 * 
034 * @see Form
035 * @tapestrydoc
036 */
037public class Errors
038{
039    /**
040     * The banner message displayed above the errors. The default value is "You must correct the
041     * following errors before
042     * you may continue.".
043     */
044    @Parameter("message:default-banner")
045    private String banner;
046
047    /**
048     * The CSS class for the div element rendered by the component. The default value is "t-error".
049     */
050    @Parameter(name = "class")
051    private String className = CSSClassConstants.ERROR;
052
053    // Allow null so we can generate a better error message if missing
054    @Environmental(false)
055    private ValidationTracker tracker;
056
057    void beginRender(MarkupWriter writer)
058    {
059        if (tracker == null)
060            throw new RuntimeException(InternalMessages.encloseErrorsInForm());
061
062        if (!tracker.getHasErrors())
063            return;
064
065        writer.element("div", "class", className);
066
067        // Inner div for the banner text
068        writer.element("div", "class", "t-banner");
069        writer.write(banner);
070        writer.end();
071
072        List<String> errors = tracker.getErrors();
073
074        if (!errors.isEmpty())
075        {
076            // Only write out the <UL> if it will contain <LI> elements. An empty <UL> is not
077            // valid XHTML.
078
079            writer.element("ul");
080
081            for (String message : errors)
082            {
083                writer.element("li");
084                writer.write(message);
085                writer.end();
086            }
087
088            writer.end(); // ul
089        }
090
091        writer.end(); // div
092
093    }
094}