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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
