[Agavi-Users] Managing duplicate array input in a custom validator

15 views
Skip to first unread message

Hash

unread,
May 20, 2012, 4:51:10 PM5/20/12
to Agavi Users Mailing List
Dear Agavi Users,

i would like to know how best to remove duplicate post data using a custom validator. The scenario is that i am posting a form relating to a 'Contact' entity which has related entities of email addresses.

Currently i have implemented a custom validator with a base="emails[]" and everything is fine until the user inputs for example, the same email address twice. The email entity is looked up twice since the validator runs for each item in the emails[] array. These entities get through to the database and i get a constraint exception in my Contact-Email link table. I would like my validator to detect and skip any duplicates in the posted data, but my validator only seems to have the scope of the current item in emails[].

I guess i could pass the whole emails array as an argument into the validator and loop over each email removing duplicates first, but then i'm not sure how to throw an error with the correct context for an email address that is invalid..

Validator: http://pastebin.com/np1m80ib
XML: http://pastebin.com/jjwQdEA5

Please advise. thank you.

Hash
_______________________________________________
users mailing list
us...@lists.agavi.org
http://lists.agavi.org/mailman/listinfo/users

Ronald Becher

unread,
May 21, 2012, 8:26:56 AM5/21/12
to us...@lists.agavi.org
Hi Hash (well, this name already fits the solution ... :) ,

You basically have two options:
- Either prepend a validator that throws out duplicates (i.e. sorts them
into a hash)
- Export a list of already created accounts (i.e, in this run) and check
against that list ... if your current email adress matches already
exists, just skip it and return the unchanged "list of already created
accounts"

Hope that helps!

Regards,
Ronald

Hash

unread,
May 21, 2012, 5:11:06 PM5/21/12
to Agavi Users Mailing List
Hello Ronald,

thanks for the suggestions ;) i think the former option would work although seems odd to have two custom validators running for one input array. I wonder if there is a way to manage the validation of the whole array (including removing duplicates and checking address validity) but set a validation error for a specific item?

thanks

Hash
Reply all
Reply to author
Forward
0 new messages