On Wed, Jul 20, 2011 at 6:33 PM, Troels Mæhl Folke
<[email protected]> wrote:
> Hi! I'm sorry if this question is pretty basic.
> I have a form, which data are used to make a database query. As there
> always has to be at least one search criteria in the query, one form
> field (it doesn't matter which one) always has to be filled. In other
> words I will have to check the form for being empty. I just don't know
> how I would accomplish this in a good way.
> I would appreciate if anyone could help me or give me a hint.
Python has a built-in any() function. It takes a list and returns True
if any of the items is True.
You could basically pass a list of validation expressions to any. For example:
if not any([not_empty(field1), not_empty(field2),
not_empty_and_email(field3)...]):
# return error
web.py form framework has form-level validation, so you can use any()
for form-level validation, and use form.validates():
myForm = form.Form(
....
validators = [
form.Validator('You need at least one field,
lambda i: any([not_empty(i.field1), not_empty(i.field2)...]),
]
)
--
Branko Vukelić
[email protected]
Lead Developer
Herd Hound (tm) - Travel that doesn't bite
www.herdhound.com
Love coffee? You might love Loveffee, too.
loveffee.appspot.com
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.