Hi!
I'd never tried to override the default ValidationDecorator.
It adds the "invalid-field" CSS class to fields that have validation
errors and to their labels. For each required field, it adds an asterisk
after its label. In addition, it writes the error message after the field
inside a <span class="error-description">
I've just did it and this is the code:
public class OtherValidationDecorator extends BaseValidationDecorator {
private final Environment environment;
private final MarkupWriter markupWriter;
public OtherValidationDecorator(Environment environment, MarkupWriter
markupWriter) {
this.environment = environment;
this.markupWriter = markupWriter;
}
@Override
public void insideField(Field field) {
if (inError(field)) {
addErrorClassToCurrentElement();
}
}
@Override
public void afterField(Field field) {
System.out.println(field.getControlName());
if (inError(field)) {
ValidationTracker tracker = getTracker();
markupWriter.element("span", "class",
"error-description");
markupWriter.write(tracker.getError(field));
markupWriter.end();
}
}
@Override
public void insideLabel(Field field, Element element) {
if (field == null)
return;
if (inError(field)) {
addErrorClassToCurrentElement();
}
if (field.isRequired()) {
element.text("*");
}
}
private boolean inError(Field field) {
ValidationTracker tracker = getTracker();
return tracker.inError(field);
}
private ValidationTracker getTracker() {
return environment.peekRequired(ValidationTracker.class);
}
private void addErrorClassToCurrentElement() {
markupWriter.getElement().addClassName("invalid-field");
}
}
In AppModule:
public static void contributeMarkupRenderer(
OrderedConfiguration<MarkupRendererFilter> configuration, final
Environment environment) {
MarkupRendererFilter validationDecorator = new MarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, MarkupRenderer
renderer) {
ValidationDecorator decorator = new
OtherValidationDecorator(environment, writer);
environment.push(ValidationDecorator.class, decorator);
renderer.renderMarkup(writer);
environment.pop(ValidationDecorator.class);
}
};
configuration.override("DefaultValidationDecorator",
validationDecorator);
}
--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
and instructor
Owner, software architect and developer, Ars Machina Tecnologia da
Informação Ltda.
http://www.arsmachina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org