Hi Thiago,

Thanks for the response: Here is the code;

import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.ValidationTracker;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.dom.Element;

/**
 * test implementation to allow for custom validation error messages below
an input field.
 *
 * Within a tml file, place something like:
 *   <label t:type="sh/FieldValidationErrorMsg" t:fieldId="inHandDate">
 *         Please enter a <i>valid date</i>
 *   </label>
 * in the vicinity of an input element with matching field id:
 *   <input type="text" t:type="textfield" t:clientId="inHandDate"
t:value="inHandDate"
 *          t:translate="string"/>
 *
 * The component will add attributes for="[fieldId]" and class="error"
(defined
 * in the corresponding tml file), and (in case the field had an error)
style="display:block"
 * to this label (CSS class error will by default hide the div).
 *
 * If parameter leaveMessage is omitted or false, the whole error div will
be removed
 * from the rendered page if the corresponding input field had no
validation error.
 * Otherwise it will be left in place (hidden through CSS) so that
client-side validation
 * may use these messages to show if so desired.
 *
 * If parameter errorId is present, the error message will only show if the
field's
 * tracker errorId matches the value of parameter errorId (so you can
define multiple
 * messages for the same field: each targeted at a specific validation
error).
 *
 * If parameter form is present, it will be used as the identifier of the
form that
 * contained the input element (so it's only relevant if there are multiple
forms
 * on a page). Otherwise, the form will be derived from the environmental
information.
 * If parameter form is used, the page's java class will need to have the
proper get
 * method for the form: public Form getMyForm() {return this.myForm;}
 *
 * If parameter labelId is present, the value will be used as id on the
label element.
 */
public class FieldValidationErrorMsg {

    @Parameter(required = true, defaultPrefix="literal")
    private String fieldId;

    @Parameter
    private Form form;

    @Parameter
    private Boolean leaveMessage; //TODO: this should ideally be a
page/flow configuration in stead of a parameter so that we can choose to
leave all error messages in place (but hidden) if those could be used for
client-side validation purposes.

    @Parameter (defaultPrefix="literal")
    private String errorId;

    @Parameter (defaultPrefix="literal")
    private String labelId;

    @Environmental(false)
    private ValidationTracker tracker;

    public void setFieldId(String fieldId) {
        this.fieldId = fieldId;
    }
    public String getFieldId() {
        return this.fieldId;
    }
    public String getLabelId() {
        return this.labelId;
    }
    public void setForm(Form form) {
        this.form = form;
    }

    public void afterRenderBody(MarkupWriter writer) {
        final String _fieldId = this.fieldId;
        Field f = new Field() {
            public String getLabel() { return null; }
            public boolean isDisabled() { return false; }
            public String getClientId() { return _fieldId; }
            public String getControlName() { return _fieldId; }
            public boolean isRequired() { return false; }
        };

        // grab the FieldValidationErrorMsg component
        Element msgEle = writer.getElement();

        ValidationTracker tracker;
        if ( this.form != null ) {
            tracker = this.form.getDefaultTracker();
        } else {
            tracker = this.tracker;
        }

        if ( (this.errorId != null && tracker.getError(f) != null &&
tracker.getError(f).contains(this.errorId))
             || (this.errorId == null && tracker.inError(f)) ) {
            // we need to display the error message, so do nothing, leave
it as is
        } else {
            // remove the error message, unless there's an instruction to
leave it in place
            if (this.leaveMessage==null || !this.leaveMessage) {
                msgEle.removeChildren();
                msgEle.pop();
            } else {
                // we're leaving it in place, but will hide it on page load
by adding a class "hidden"
                String curClassValue = msgEle.getAttribute("class");
                String[] classValues = {"class","hidden "+curClassValue};
                msgEle.forceAttributes(classValues);
            }
        }
    }

}


and FieldValidationErrorMsg.tml for the same:

<?xml version="1.0" encoding="UTF-8"?>
<html for="${fieldId}" class="error" generated="false" xmlns:t="
http://tapestry.apache.org/schema/tapestry_5_1_0.xsd";>
    <t:if test="labelId">
        <t:sh.common.addAttribute name="id" value="${labelId}"/>
    </t:if>
    <t:body />
</html>


Thanks,
Rukmini

On Tue, Aug 13, 2013 at 9:55 AM, Thiago H de Paula Figueiredo <
thiag...@gmail.com> wrote:

> On Tue, 13 Aug 2013 13:48:29 -0300, rukmini n <rukmini...@gmail.com>
> wrote:
>
>  Hi all,
>>
>
> Hi!
>
>
>  I get the below exception while rendering tml file.
>>
>
> What's the Java source code for FieldValidationErrorMsg?
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: 
> users-unsubscribe@tapestry.**apache.org<users-unsubscr...@tapestry.apache.org>
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>

Reply via email to