NOTE: This information only applies to older versions of Tapestry. For Tapestry 5 see http://tapestry.apache.org/input-validation.html

If you have a custom validation delegate (class that extends ValidationDelegeate) you may want to display a required field marker next to the field when the page is presented to your visitors.

If you are migrating a 3.0 application you will want to override writeSuffix() like this:

public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
                if(component.getBinding("validator")!=null) {
                        ExpressionBinding expressionBinding = (ExpressionBinding) component.getBinding("validator");
                        BaseValidator singleValidator = (BaseValidator) expressionBinding.getObject();
                        if(singleValidator.isRequired()) {

                          writer.printRaw("&nbsp");
                          writer.begin("span");
                          writer.attribute("class", "required-marker");
                          writer.beginEmpty("img");
                          writer.attribute("src","images/required.jpg");
                          writer.attribute("alt","required field");
                          writer.attribute("align","middle");
                          writer.end();
                        }
                }
}

However, if you are using the new Validation model of Tapestry 4.0 you will want to do this

public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
                        boolean requiredField = false;
                        if(component.getBinding("validators") instanceof ExpressionBinding) {
                                ExpressionBinding expressionBinding = (ExpressionBinding) component.getBinding("validators");
                                org.apache.tapestry.form.validator.BaseValidator singleValidator = (org.apache.tapestry.form.validator.BaseValidator)expressionBinding.getObject();
                                if(singleValidator instanceof Required) {
                                  requiredField = true;
                                }                                                               
                        } else {
                                ValidatorsBinding vb = (ValidatorsBinding) component.getBinding("validators");
                                Collection collection = (Collection) vb.getObject();
                                for(Iterator it = collection.iterator();it.hasNext();){
                                    Validator validatorObject = (Validator) it.next();
                                    if(validatorObject instanceof Required) {
                                      requiredField = true;
                                    }
                               }                        
                        }  

                if(requiredField) {
                        writer.printRaw("&nbsp");
                        writer.begin("span");
                        writer.attribute("class", "required-marker");
                        writer.beginEmpty("img");
                        writer.attribute("src","images/required.jpg");
                        writer.attribute("alt","required field");
                        writer.attribute("align","middle");
                        writer.end();
                }
                
                if(isInError()) {
                        writer.printRaw("&nbsp");
                        writer.beginEmpty("img");
                        writer.attribute("align","middle");
                        writer.attribute("src","images/warning_icon.gif");
                }

HowardLewisShip: In Tapestry 4.0, IFormComponent has a isRequired() method for this purpose. Iternally, it's kind of like your solution (but doesn't using OGNL or reflection).

public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
                       
                if(field.isRequired()) {
                        writer.printRaw("&nbsp");
                        writer.begin("span");
                        writer.attribute("class", "required-marker");
                        writer.beginEmpty("img");
                        writer.attribute("src","images/required.jpg");
                        writer.attribute("alt","required field");
                        writer.attribute("align","middle");
                        writer.end();
                }
                
                if(isInError()) {
                        writer.printRaw("&nbsp");
                        writer.beginEmpty("img");
                        writer.attribute("align","middle");
                        writer.attribute("src","images/warning_icon.gif");
                }

CheckingForRequiredValidator (last edited 2011-01-02 01:12:47 by BobHarner)