I checked the wiki.  The basic technique is already there, at the bottom
of this page:
http://wiki.apache.org/tapestry/CheckingForRequiredValidator

I learned that the code could be simplified.  My isRequired() method --
left over from an earlier Tapestry version -- wasn't needed.  The
writeAttributes() method boils down to:

      if (isInError()) {
          writer.attribute("class", "error");
      }
      else if (component.isRequired()) {
          writer.attribute("class", "required-field");
      }






Andreas Bulling wrote:

>Nice one - thanks for that! ;)
>Perhaps that's something for the Wiki...
>
>Andreas
>
>On 14. Jun 2006 - 17:18:28, Bryan Lewis wrote:
>| Here's what's been working well for us.  Our custom ValidationDelegate
>| extends Tapestry's ValidationDelegate and overrides the
>| writeAttributes() method, which is documented as "Does nothing. Override
>| in a subclass to decorate fields."  We emit a
>| class="required-field" attribute.  It's up to the app's stylesheet to
>| define the appearance.
>| 
>|        public void writeAttributes(IMarkupWriter writer,
>|                                 IRequestCycle cycle,
>|                                 IFormComponent component,
>|                                 IValidator validator)
>|     {
>|         if (isInError()) {
>|             writer.attribute("class", "error");
>|         }
>|         else if (isRequired(component)) {
>|             writer.attribute("class", "required-field");
>|         }
>|     }
>| 
>|     /** Returns true if the component has a Required validator. */
>|     public boolean isRequired(IFormComponent component)
>|     {
>|         boolean retval = false;
>| 
>|         if (component instanceof ValidatableField) {
>|             Object validators = ((ValidatableField)
>| component).getValidators();
>|             if (validators != null
>|                 // The docs say getValidators() is "coerced into an Iterator
>|                 // of Validators", but I get a Collection.  Allow for
>| either.
>|                 Iterator it = null;
>|                 if (validators instanceof Iterator) {
>|                     it = (Iterator) validators;
>|                 }
>|                 else if (validators instanceof Collection) {
>|                     it = ((Collection) validators).iterator();
>|                 }
>|                 assert it != null;
>| 
>|                 while (it.hasNext()) {
>|                     Validator validator = (Validator) it.next();
>|                     if (validator.isRequired()) {
>|                         retval = true;
>|                         break;
>|                     }
>|                 }
>|             }
>|         }
>|         return retval;
>|     }
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to