Java

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Indicating required fields to user?

    3 answers - 772 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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: users-unsubscribe (AT) tapestry (DOT) apache.org
    For additional commands, e-mail: users-help (AT) tapestry (DOT) apache.org
  • No.1 | | 2640 bytes | |

    Here's what's been working well for us. custom ValidationDelegate
    extends Tapestry's ValidationDelegate and overrides the
    writeAttributes() method, which is documented as "Does nothing.
    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) {
    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: users-unsubscribe (AT) tapestry (DOT) apache.org
    >For additional commands, e-mail: users-help (AT) tapestry (DOT) apache.org
    >


    To unsubscribe, e-mail: users-unsubscribe (AT) tapestry (DOT) apache.org
    For additional commands, e-mail: users-help (AT) tapestry (DOT) apache.org
  • No.2 | | 1915 bytes | |

    Nice one - thanks for that! ;)
    Perhaps that's something for the Wiki

    Andreas

    14. Jun 2006 - 17:18:28, Bryan Lewis wrote:
    | Here's what's been working well for us. custom ValidationDelegate
    | extends Tapestry's ValidationDelegate and overrides the
    | writeAttributes() method, which is documented as "Does nothing.
    | 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) {
    | 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: users-unsubscribe (AT) tapestry (DOT) apache.org
    For additional commands, e-mail: users-help (AT) tapestry (DOT) apache.org
  • No.3 | | 2867 bytes | |

    I checked the wiki. The basic technique is already there, at the bottom
    of this page:

    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
    >

    14. Jun 2006 - 17:18:28, Bryan Lewis wrote:
    >| Here's what's been working well for us. custom ValidationDelegate
    >| extends Tapestry's ValidationDelegate and overrides the
    >| writeAttributes() method, which is documented as "Does nothing.
    >| 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) {
    >| 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: users-unsubscribe (AT) tapestry (DOT) apache.org
    >For additional commands, e-mail: users-help (AT) tapestry (DOT) apache.org
    >


    To unsubscribe, e-mail: users-unsubscribe (AT) tapestry (DOT) apache.org
    For additional commands, e-mail: users-help (AT) tapestry (DOT) apache.org

Re: Indicating required fields to user?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 3 + 3 =
QUESTION ON "Java"

EMSDN.COM