You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Eduardo Samboal <es...@telcomunity.com> on 2004/06/04 11:09:06 UTC

Help with validation

Hi, i'm using Tapestry since a few weeks ago, and I'm getting some 
difficulties in validating a couple of related fields inside a ListEdit 
component.
Here is the description of what i'm trying to implement:

I have two input fields, a text field implemented with a ValidField 
component and a select drop-down box implemented with a PropertySelection 
component. The first one, of course, has a connected FieldLabel component. 
These three components are nested inside a ListEdit component, so they can 
be rendered a number of times decided by the application data. The 
validation process involves the following steps:
- First, checking that the text field is not blank. If it's blank, then the 
value of the PropertySelection component is omitted
- Second, if it isn't blank, then it's checked against some validation 
rules. Of course, both step 1 and 2 are implemented using a custom validator.
- Third, if the text field isn't blank and is valid, I need to check the 
value of the PropertySelection component, and here is my problem.
	I have to use the form listener, since a PropertySelection doesn't admit a 
custom validator, and, furthermore, I'm trying to make a cross field 
validation. In the form listener, I can get the list of fields used by the 
ListEdit component and do the check against them, but I don't know how I 
can get a reference to the correct component to record it as an invalid 
field in the ValidatorDelegate. I mean, I have ways to know that the data 
in the object with i.e. ID=3 has invalid data, and I want that in the 
rendering of the page the component who actually rendered that data shows 
an error marker, but I don't know how.

Also, I've noticed that when a validation against one of the ValidFields 
fails, the label that displays the error it's not it's matching label, but 
the label of the next field in the series... could it be some kind of bug 
or i'm doing something wrong ?

Thanks is advance.
Eduardo Samboal.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Help with validation

Posted by Galen Meurer <si...@galenmeurer.com>.
In order to get a PropertySelect to decorate, I used composition, creating
our own wrapper for PropertySelect.

I created a component that takes a validator as a parameter. I didn't want
it to be required, though, and I needed it to be auto, so I created a
stand-in silly validator, "notRequired" that basically just does a lot of
nothing.

  <parameter name="validator"
             type="org.apache.tapestry.valid.IValidator"
             required="no"
             direction="auto"
             default-value="beans.notRequired"/>

Then in that component, I declare a PropertySelection, passing on some
params to it:

  <component id="selectPicklist" type="PropertySelection">
    <binding name="model" expression="getPropertySelectionModel(picklist)"/>
    <binding name="value" expression="currentSelection"/>
    <inherited-binding name="submitOnChange"
parameter-name="submitOnChange"/>
    <inherited-binding name="disabled" parameter-name="disabled"/>
  </component>

In the Java for the wrapper component, you'll need to implement
renderComponent:

  protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
{
    IValidationDelegate delegate = getForm(cycle).getDelegate();
    IValidator validator = getValidator();
    boolean rendering = !cycle.isRewinding();

[...]
    if (rendering && (delegate != null) && (validator != null)) {
      delegate.writePrefix(writer, cycle, this, validator);
[...]
    IComponent composedComponent = getComponent("selectPicklist");
    composedComponent.render(superWriter,cycle);

    if (rendering && (delegate != null) && (validator != null)) {
      delegate.writeSuffix(writer, cycle, this, validator);
      myWriter.clear();
    }
 }

Just wrap the render of the PropertySelect with calls to your delegate to
decorate. If you also need validator contribution to the field rendering,
you will need to create your own writer (not pictured here) to pass to the
composed component.

You'll be hardcoding the ID of the composed Picklist, but this knowledge is
well encapsulated given that it's knowledge about its own contained
component.

Look at the source for ValidField for another example (and more
information). Also, for more on your own writer and some more examples of
this sort of thing, check out the source for Peter Butler's valid
components: http://www.clever.co.nz/resources.html

Galen;

----- Original Message ----- 
From: "Eduardo Samboal" <es...@telcomunity.com>
To: <ta...@jakarta.apache.org>
Sent: Friday, June 04, 2004 2:09 AM
Subject: Help with validation


> Hi, i'm using Tapestry since a few weeks ago, and I'm getting some
> difficulties in validating a couple of related fields inside a ListEdit
> component.
> Here is the description of what i'm trying to implement:
>
> I have two input fields, a text field implemented with a ValidField
> component and a select drop-down box implemented with a PropertySelection
> component. The first one, of course, has a connected FieldLabel component.
> These three components are nested inside a ListEdit component, so they can
> be rendered a number of times decided by the application data. The
> validation process involves the following steps:
> - First, checking that the text field is not blank. If it's blank, then
the
> value of the PropertySelection component is omitted
> - Second, if it isn't blank, then it's checked against some validation
> rules. Of course, both step 1 and 2 are implemented using a custom
validator.
> - Third, if the text field isn't blank and is valid, I need to check the
> value of the PropertySelection component, and here is my problem.
> I have to use the form listener, since a PropertySelection doesn't admit a
> custom validator, and, furthermore, I'm trying to make a cross field
> validation. In the form listener, I can get the list of fields used by the
> ListEdit component and do the check against them, but I don't know how I
> can get a reference to the correct component to record it as an invalid
> field in the ValidatorDelegate. I mean, I have ways to know that the data
> in the object with i.e. ID=3 has invalid data, and I want that in the
> rendering of the page the component who actually rendered that data shows
> an error marker, but I don't know how.
>
> Also, I've noticed that when a validation against one of the ValidFields
> fails, the label that displays the error it's not it's matching label, but
> the label of the next field in the series... could it be some kind of bug
> or i'm doing something wrong ?
>
> Thanks is advance.
> Eduardo Samboal.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org