On Apr 26, 9:11 am, Johann Spies <[email protected]> wrote:
> The definition:
>
> db.define_table("navrae",
>     Field("begindatum", "date", default=None),
>     Field("einddatum", "date", default=None),
>     Field("gebruiker", "string", length=8, notnull=True, default='NULL'),
>     Field("ipadres", "string", length=15, notnull=True, default='NULL'),
>     Field("datum_van_navraag", "datetime", default=None),
>     Field("navraag_deur_wie", "string", default=None))
>
> I want to check two things in this form:
>
> begindatum =< einddatum
> Only one of gebruiker and ipadres may be used.
>
> I have tried unsucessfully accessing the variables in the function like this:
>
>     if response.vars.begindatum > response.vars.einddatum:
>         message = T("end date before start date")
>
> and
>
>     if form.vars.begindatum > response.vars.einddatum:
>         message = T("end date before start date")
>
> and
>
>     if begindatum > einddatum:
>         message = T("end date before start date")
>
> But none of those variables are available.  When I try
> form = SQLFORM(db.navrae)
>     message = T("form has errors")
>     if request.vars.begindatum > request.vars.einddatum:
>         message = T("end date before start date")
>         form.errors == True
>     if form.errors:
>         response.flash = message
>     elif form.accepts(request.vars, session):
>         form.vars.id = db.navrae.insert(**dict(form.vars))
>         response.flash = 'form accepted'
>     else:
>         response.flash = 'please fill out the form'
>
> I get no error message and the form is accepted even when einddatum <
> begindatum.

Your controller is called two times:   once with the request, and once
with the response.

form.accepts()  basically tells you which call it is:  if "accepts()"
fails, it means it is the initial request, and your contorller is just
beginning communication with the client:  you are forming the HTML,
etc. you will send out to the client to display;

If form.accepts()  succeeds, it means that response variables are set
- AND form validation passed.   If it fails, you _always_ set response
variables in response - which is how you re-send something to the
client (if it is the initial request, setting the response things has
no effect, is ok).

This is "convenient" - but hides some of the program flow from you.
Once you understand this "called twice" aspect, you should understand
better how to write the code (and forget the double aspect).

So .... you have a stream something like this:

...client "calls" a URL (your app / controller);
web2py main sets context (runs your models, calls your controller
first time)
your controller returns with a dict (returns to main(), which does
what's needed to send this to client)
...client gets page
...client responds to any forms (submits page - same url)
web2py main sets context, calls your controller (this time with
resopnse variables set for you)
your controller gets a second call - and does form.accepts() - and
either re-displays this page (above: back to same URL for client), or
redirects (new URL, new controller call...)

Hope this makes some sense now:
In your code above, your are checking request vars - these are not set
by the client - they are sent _to_ the client (i.e. are empty, or as
you set them);
Next, you check form.errors - which are the ones potentially set by
all the run validators... which form.accepts()   (!) runs for you
THEN you try a form accepts....

After you have digested this some,  have a look at
http://web2py.com/book/default/section/7/2?search=form.accepts;
If you want, also look at the docstrings for FORM in gluon/html.py
(SQLFORM extends FORM).

I hope you find this helpful.
Regards,
- Yarko

>
> I also tried this in the model:
>
> db.navrae.einddatum.requires= (db.navrae.einddatum >= db.navrae.begindatum)
>
> but get a TypeError: 'Query' object is not callable
>
> So how do I do it?
> --
> "Finally, brethren, whatsoever things are true,  whatsoever things are
> honest, whatsoever things are  just, whatsoever things are pure,
> whatsoever things are lovely, whatsoever things are of good report; if
> there be any virtue, and if there be any praise, think on these
> things."    Philippians 4:8
>
> --
> Subscription settings:http://groups.google.com/group/web2py/subscribe?hl=en

Reply via email to