tapestry5 reporting messages when the ValidationTracker is not available

Sometimes, you run into some tricky problems. I have some pages which need to be externally invocable using parameters. This is perfectly handled in tapestry using the activation context and or extraction of extra parameters from the Request object. However, when handling this information, I may raise a problem and this needs to be reported. This should be done using the normal error reporting in the component.

The solution I built for this (and will be part of part of the next release (0.9.1) of equanda-tapestry5 components is the ValidationTrackerDelegate. This allows you to post your message in the tapestry Environment, and the message is retrieved from that to be displayed on your page. For this to work, your page template should contain something like (note that the order of the components is important).

<t:form>
    <t:equanda.ValidationTrackerDelegate/>
    <t:errors/>

    .....

</t:form>

In your page, the message need to be stored in the environment in the beginRender event. So your page code will look something like

@Inject
private Environment environment;

private String keepMessage;

public void onActivate()
{
    .... do whatever needs to be done

    .... if there is a problem do the following
    keepMessage = "error message";
}

void beginRender()
{
    if ( null != keepMessage ) environment.push( MessageContainer.class, new MessageContainer( keepMessage ) );
}

void endRender()
{
    if ( null != keepMessage ) environment.pop( MessageContainer.class );
}

The actual component is very simple. We need a class to store the message in the environment (aptly named MessageContainer in this example).

public class MessageContainer
{
    private String message;

    public MessageContainer( String message )
    {
        this.message = message;
    }

    public String getMessage()
    {
        return message;
    }
}

The actual component then becomes almost trivial. From inside the form (where the ValidationTracker is available, and before it is used in the errors component), the following is done.

public class ValidationTrackerDelegate
{
    @Inject
    private Environment environment;

    @Environmental
    private ValidationTracker validationTracker;

    boolean beginRender()
    {
        MessageContainer mc = environment.peek( MessageContainer.class );

        if ( mc != null )
        {
            validationTracker.recordError( mc.getMessage() );
        }

        return false;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

*