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