You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Foror <fo...@mail.ru> on 2007/07/11 06:00:47 UTC

How to create my ValidationDecorator in T5?

Hi! How to create my ValidationDecorator in T5, with marked required
fields when Form is first loaded on page?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to create my ValidationDecorator in T5?

Posted by devilabit <tj...@gmail.com>.
We had a requirement to put an image after each label for form fields that
were required.  

I am new to tapestry but after some investigation and by following Howard's
pattern I was able to do this.

Firstly I decorated the DefaultValidationDecorator class and added the image
inside the label.  I tired passing in the markupWriter to the class and
using it to construct the markup but I couldnt get that to work.  Instead I
used the direct access to the element to add the sub element.

public class RequiredFieldValidationDecorator implements ValidationDecorator
{

	private ValidationDecorator decorator;	

	private final Asset iconAsset;

	private final Messages validationMessages;

	public RequiredFieldValidationDecorator(ValidationDecorator decorator,
Messages validationMessages, Asset iconAsset) {
		this.decorator = decorator;
		this.validationMessages = validationMessages;
		this.iconAsset = iconAsset;
	}

	public void insideField(Field field) {
		decorator.insideField(field);
	}

	public void beforeLabel(Field field) {
		decorator.beforeLabel(field);
	}

	public void insideLabel(Field field, Element element) {
		decorator.insideLabel(field, element);
		if (field.isRequired()) {
			String iconId = field.getClientId() + ":required_field_icon";

			element.raw(" <em>");
			element.element("img",

							"src", iconAsset.toClientURL(),

							"alt", validationMessages.get("REQUIRED_FIELD_IMG_ALT"),

							"id", iconId);
			element.raw("</em>");
		}
	}

	public void afterLabel(Field field) {
		decorator.afterLabel(field);
	}

	public void beforeField(Field field) {
		decorator.beforeField(field);
	}

	public void afterField(Field field) {
		decorator.afterField(field);
	}
}


Then i changed my default layout to add this new
RequiredFieldValidationDecorator to the environment.  Now anytime a field is
marked as required (ie t:validate="required" in your tml) the required star
is rendered after the label.


	@Inject
	private Environment environment;

	@Inject
	@Path("context:/images/required_star.gif")
	private Asset requiredFieldStar;

	@BeginRender
	public void beginRender() {
		ValidationDecorator decorator =
environment.peek(ValidationDecorator.class);
		environment.push(ValidationDecorator.class, new
RequiredFieldValidationDecorator(decorator, messages, requiredFieldStar));
	}

	@AfterRender
	public void afterRender() {
		environment.pop(ValidationDecorator.class);
	}

Hope this might help someone.

_devilabit_


Howard Lewis Ship wrote:
> 
> You can create your own implementation of the interface, or subclass
> Tapestry's and extend it.
> 
> The trick is to get Tapestry to *use* it.
> 
> If you look at the various form components, they have the following:
> 
> @Environmental
> private ValidationDecorator _decorator;
> 
> That means "pick up the decorator from the environment".  What's the
> environment?
> 
> It's a service, Environment, that is scoped to the request, that holds
> these things.
> 
> Before the Form renders, your decorator must be added to the environment:
> 
> Perhaps you would do this from your application-specific Layout component:
> 
> @Inject
> private Environment _environment;
> 
> public void beginRender()
> {
>   _environment.push(ValidationDelegate.class, new MyValidationDelegate());
> }
> 
> public void afterRender()
> {
>   _environment.pop(ValidationDelegate.class);
> }
> 
> 
> This Layout component (T4 used the term "Border" for this pattern)
> wraps around all the content of your page.  It generally provides some
> navigation for the page, as well as CSS & etc . for look and feel;
> here is is also providing the ValidatonDelegate used inside your page.
> 
> On 7/10/07, Foror <fo...@mail.ru> wrote:
>> Hi! How to create my ValidationDecorator in T5, with marked required
>> fields when Form is first loaded on page?
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
> 
> 
> -- 
> Howard M. Lewis Ship
> TWD Consulting, Inc.
> Independent J2EE / Open-Source Java Consultant
> Creator and PMC Chair, Apache Tapestry
> Creator, Apache HiveMind
> 
> Professional Tapestry training, mentoring, support
> and project work.  http://howardlewisship.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-create-my-ValidationDecorator-in-T5--tp11533833p18713492.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to create my ValidationDecorator in T5?

Posted by Howard Lewis Ship <hl...@gmail.com>.
You can create your own implementation of the interface, or subclass
Tapestry's and extend it.

The trick is to get Tapestry to *use* it.

If you look at the various form components, they have the following:

@Environmental
private ValidationDecorator _decorator;

That means "pick up the decorator from the environment".  What's the
environment?

It's a service, Environment, that is scoped to the request, that holds
these things.

Before the Form renders, your decorator must be added to the environment:

Perhaps you would do this from your application-specific Layout component:

@Inject
private Environment _environment;

public void beginRender()
{
  _environment.push(ValidationDelegate.class, new MyValidationDelegate());
}

public void afterRender()
{
  _environment.pop(ValidationDelegate.class);
}


This Layout component (T4 used the term "Border" for this pattern)
wraps around all the content of your page.  It generally provides some
navigation for the page, as well as CSS & etc . for look and feel;
here is is also providing the ValidatonDelegate used inside your page.

On 7/10/07, Foror <fo...@mail.ru> wrote:
> Hi! How to create my ValidationDecorator in T5, with marked required
> fields when Form is first loaded on page?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org