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;
    }



Phillip Rhodes wrote:

>I am using tapestry validation successfully for required fields.  All
>works well.  However, I need to display a required indicator to the user
>so that they know before submitting the form that these fields are
>required.  Currently, users must submit the form to enable the "required"
>decorations.
>
>It would be nice if I could toggle on the validation decorations in the
>form on the initial presentation of the form to the user, so they don't
>have to guess which fields are required when they are filling out the form
>the first time.
>
>any pointers would be appreciated.
>
>
>
>
>
>---------------------------------------------------------------------
>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