001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.corelib.components;
014
015import org.apache.tapestry5.BindingConstants;
016import org.apache.tapestry5.ComponentResources;
017import org.apache.tapestry5.Field;
018import org.apache.tapestry5.MarkupWriter;
019import org.apache.tapestry5.SymbolConstants;
020import org.apache.tapestry5.annotations.HeartbeatDeferred;
021import org.apache.tapestry5.annotations.Parameter;
022import org.apache.tapestry5.annotations.SupportsInformalParameters;
023import org.apache.tapestry5.dom.Element;
024import org.apache.tapestry5.ioc.annotations.Inject;
025import org.apache.tapestry5.ioc.annotations.Symbol;
026
027/**
028 * Provides a client-side element to contain validation errors; this renders as a {@code <p class="help-block">}.
029 * Must be enclosed by a
030 * {@link org.apache.tapestry5.corelib.components.Form} component and assumes the field and the Error component
031 * are enclosed by a {@code <div class="[form group CSS class]">}, where {@code [form group CSS class]}
032 * is defined by the value of the {@link SymbolConstants#FORM_GROUP_WRAPPER_CSS_CLASS}
033 * configuration symbol ({@code tapestry.form-group-wrapper-css-class}).
034 *
035 * It is acceptable to include multiple Errors components for a single field; this is sometimes necessary
036 * when creating a responsive layout - which should probably ensure that only one of the Errors is
037 * visible at any time.
038 *
039 * Errors is optional, and Tapestry's client-side logic will do a reasonable job of placing a help block
040 * dynamically when a validation error must be presented; this component is intended for use when the default logic
041 * doesn't place the help block in the right spot.
042 *
043 * @tapestrydoc
044 * @since 5.2.0
045 */
046@SupportsInformalParameters
047public class Error
048{
049    /**
050     * The for parameter is used to identify the {@link Field} to present errors of.
051     */
052    @Parameter(name = "for", required = true, allowNull = false, defaultPrefix = BindingConstants.COMPONENT)
053    private Field field;
054
055    @Inject
056    private ComponentResources resources;
057    
058    @Inject
059    @Symbol(SymbolConstants.ERROR_CSS_CLASS)
060    private String cssClass;
061
062    boolean beginRender(final MarkupWriter writer)
063    {
064        // Initially invisible; will be shown on client if an error exists.
065        Element element = writer.element("p", "class", 
066                !("help-block".equals(cssClass)) ? ("help-block " + cssClass) : cssClass + " invisible",
067                        "role", "alert");
068        
069        resources.renderInformalParameters(writer);
070
071        // Wait until the end of the heartbeat to ensure the Field has had a chance to render.
072        updateElement(element);
073
074        writer.end();
075
076        return false;
077    }
078
079    @HeartbeatDeferred
080    private void updateElement(final Element element)
081    {
082        // The field may add an id attribute because of this call.
083        element.attribute("data-error-block-for", field.getClientId());
084        String id = field.getClientId() + "-help-block";
085        element.attribute("id", id);
086        Element input = element.getDocument().getElementById(field.getClientId());
087        if (input != null) {
088            input.attribute("aria-describedby", id);
089        }
090    }
091}