001// Copyright 2010-2013 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.BindingConstants; 018import org.apache.tapestry5.ComponentResources; 019import org.apache.tapestry5.Field; 020import org.apache.tapestry5.MarkupWriter; 021import org.apache.tapestry5.annotations.HeartbeatDeferred; 022import org.apache.tapestry5.annotations.Parameter; 023import org.apache.tapestry5.annotations.SupportsInformalParameters; 024import org.apache.tapestry5.dom.Element; 025import org.apache.tapestry5.ioc.annotations.Inject; 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">}. 032 * <p/> 033 * It is acceptable to include multiple Errors components for a single field; this is sometimes necessary 034 * when creating a responsive layout - which should probably ensure that only one of the Errors is 035 * visible at any time. 036 * <p/> 037 * Errors is optional, and Tapestry's client-side logic will do a reasonable job of placing a help block 038 * dynamically when a validation error must be presented; this component is intended for use when the default logic 039 * doesn't place the help block in the right spot. 040 * 041 * @tapestrydoc 042 * @since 5.2.0 043 */ 044@SupportsInformalParameters 045public class Error 046{ 047 /** 048 * The for parameter is used to identify the {@link Field} to present errors of. 049 */ 050 @Parameter(name = "for", required = true, allowNull = false, defaultPrefix = BindingConstants.COMPONENT) 051 private Field field; 052 053 @Inject 054 private ComponentResources resources; 055 056 boolean beginRender(final MarkupWriter writer) 057 { 058 // Initially invisible; will be shown on client if an error exists. 059 Element element = writer.element("p", "class", "help-block invisible"); 060 061 resources.renderInformalParameters(writer); 062 063 // Wait until the end of the heartbeat to ensure the Field has had a chance to render. 064 updateElement(element); 065 066 writer.end(); 067 068 return false; 069 } 070 071 @HeartbeatDeferred 072 private void updateElement(final Element element) 073 { 074 // The field may add an id attribute because of this call. 075 element.attribute("data-error-block-for", field.getClientId()); 076 } 077}