You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Denis Souza <ds...@sti.com.br> on 2005/04/01 04:30:36 UTC
Unique Validator in Foreach loop
Hi,
I have a Foreach loop in which I want to validate the fields inside it. For
any regular required or minimum-length validation that's fine, but I'm
trying to build a validator that checks if each field is unique. For
example, let's say the only field I have inside the foreach loop is a field
for a name. Let's say this loop iterates 5 times so that there would be 5
text fields rendered. I would like each of these "names" to be unique.
What I have done so far is, I created a validator called UniqueValidator and
I'm passing it the same list of objects to perform the validation that I use
in the foreach loop. This implementation causes two problems:
1) Tapestry is updating the list of objects with the submitted values only
after the validations run, so I'm actually checking for uniqueness in a
deprecated list. I have to be able to see the values that were just
submitted in case the user changes more than one value.
2) I can't tell where in the object list is the field that is being
validated, so I can't skip the item on the list that is being checked and I
end up returning validation errors even if a name is entered only once in
the list. I can work around this by checking if there is more than one
object in the list that is the same as the input, but I don't think it's
such an elegant solution.
Any ideas?
Thanks,
Denis
RE: Unique Validator in Foreach loop
Posted by Denis Souza <ds...@sti.com.br>.
I hadn't thought of that.
Thanks!
Denis
-----Original Message-----
From: Paul Ferraro [mailto:pmf8@columbia.edu]
Sent: sexta-feira, 1 de abril de 2005 01:05
To: Tapestry users
Subject: Re: Unique Validator in Foreach loop
Off the top of my head, try something like this:
public class UniqueValidator implements IValidator
{
private Set valueSet = new HashSet();
public toObject(IFormComponent field, String input) throws
ValidatorException
{
// Preliminary validation
// Validate uniqueness
if (valueSet.contains(input))
{
// Duplicate field!
throw new ValidatorException(field.getDisplayName() + " must
be unique.");
}
valueSet.add(input);
}
// ...
}
Then make sure that your UniqueValidator bean is request scoped.
Paul
Denis Souza wrote:
>Hi,
>
>
>
>I have a Foreach loop in which I want to validate the fields inside it. For
>any regular required or minimum-length validation that's fine, but I'm
>trying to build a validator that checks if each field is unique. For
>example, let's say the only field I have inside the foreach loop is a field
>for a name. Let's say this loop iterates 5 times so that there would be 5
>text fields rendered. I would like each of these "names" to be unique.
>
>
>
>What I have done so far is, I created a validator called UniqueValidator
and
>I'm passing it the same list of objects to perform the validation that I
use
>in the foreach loop. This implementation causes two problems:
>
>
>
>1) Tapestry is updating the list of objects with the submitted values only
>after the validations run, so I'm actually checking for uniqueness in a
>deprecated list. I have to be able to see the values that were just
>submitted in case the user changes more than one value.
>
>
>
>2) I can't tell where in the object list is the field that is being
>validated, so I can't skip the item on the list that is being checked and I
>end up returning validation errors even if a name is entered only once in
>the list. I can work around this by checking if there is more than one
>object in the list that is the same as the input, but I don't think it's
>such an elegant solution.
>
>
>
>Any ideas?
>
>
>
>Thanks,
>
>Denis
>
>
>
>
>
>
---------------------------------------------------------------------
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
Re: Unique Validator in Foreach loop
Posted by Paul Ferraro <pm...@columbia.edu>.
Off the top of my head, try something like this:
public class UniqueValidator implements IValidator
{
private Set valueSet = new HashSet();
public toObject(IFormComponent field, String input) throws
ValidatorException
{
// Preliminary validation
// Validate uniqueness
if (valueSet.contains(input))
{
// Duplicate field!
throw new ValidatorException(field.getDisplayName() + " must
be unique.");
}
valueSet.add(input);
}
// ...
}
Then make sure that your UniqueValidator bean is request scoped.
Paul
Denis Souza wrote:
>Hi,
>
>
>
>I have a Foreach loop in which I want to validate the fields inside it. For
>any regular required or minimum-length validation that's fine, but I'm
>trying to build a validator that checks if each field is unique. For
>example, let's say the only field I have inside the foreach loop is a field
>for a name. Let's say this loop iterates 5 times so that there would be 5
>text fields rendered. I would like each of these "names" to be unique.
>
>
>
>What I have done so far is, I created a validator called UniqueValidator and
>I'm passing it the same list of objects to perform the validation that I use
>in the foreach loop. This implementation causes two problems:
>
>
>
>1) Tapestry is updating the list of objects with the submitted values only
>after the validations run, so I'm actually checking for uniqueness in a
>deprecated list. I have to be able to see the values that were just
>submitted in case the user changes more than one value.
>
>
>
>2) I can't tell where in the object list is the field that is being
>validated, so I can't skip the item on the list that is being checked and I
>end up returning validation errors even if a name is entered only once in
>the list. I can work around this by checking if there is more than one
>object in the list that is the same as the input, but I don't think it's
>such an elegant solution.
>
>
>
>Any ideas?
>
>
>
>Thanks,
>
>Denis
>
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org