You can also do
db.table.field1.requires=IS_NULL_OR(IS_IPV4())
db.table.field2.requires=IS_NULL_OR(IS_IPV4())
def at_least_one(form):
if not form.vars.field1 and not form.vars.field2:
form.errors.field2='cannot be empty if field1 is empty'
form.errors.field1='cannot be empty if field2 is empty'
if form.accepts(request.vars,session,onvalidation=at_least_one): ....
In this case it is general, the call is gone inside accept, you do not
need dbio=False and you can associate the errors to any field you
want. The check is performed on values that have already been
filtered by normal validation.
onvalidation is executed after regular validation, but before dbio. If
onvalidation sets an error, no dbio is performed, and accepts returns
false.
This is not in the book because we introduced sometime in fall 2009.
massimo
On Apr 2, 10:10 pm, Jonathan Lundell <[email protected]> wrote:
> On Apr 2, 2010, at 8:05 PM, mdipierro wrote:
>
> > Yes. No one solution is obviously better. It is good to have some
> > options.
>
> Yarko's approach allows for a more general error message, at the expense of
> not associating it with a particular input field. In my case, I think I see a
> way of phrasing the error messages so that it works OK with Massimo's
> approach.
>
>
>
> > Massimo
>
> > On Apr 2, 8:45 pm, Yarko Tymciurak <[email protected]>
> > wrote:
> >> On Apr 2, 7:14 pm, mdipierro <[email protected]> wrote:
>
> >>> db.table.field1.requires=IS_NULL_OR(IS_IPV4())
> >>> db.table.field2.requires=IS_NULL_OR(IS_IPV4()) if request.vars.field1
> >>> else IS_IPV4()
>
> >> Thank you for this, Massimo - this is a nice, compact idiom for using
> >> existing validators; I hadn't considered (don't know why not - it's
> >> obvious looking at it) this idiom in assigning to a "requires" field.
>
> >> My approach is more general - ANY situation where you want to do some
> >> combinatorial logic, or external checking: form.accepts( ...,
> >> dbio=False)...
>
> >> Good question, Jonathan! :-)
>
> >> - Yarko
>
> >>> On Apr 2, 10:32 am, Jonathan Lundell <[email protected]> wrote:
>
> >>>> I think I've seen an answer to this one, but I can't find it.
>
> >>>> I've got a form with two Field's, both IS_IPV4(). The validation I'm
> >>>> after is that one or both must be present; the only invalid case is both
> >>>> of them null.
>
> >>>> How do I write that?
--
You received this message because you are subscribed to the Google Groups
"web2py-users" 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/web2py?hl=en.