On Mar 2, 2010, at 10:46 AM, [email protected] wrote:
> I have a form with a string field that uses the IS_IN_SET validator.
> The set of strings that is allowed must be generated dynamically each
> time the form is generated. So I add the validator in the controller.
> ( See the snippet below. )
>
> The problem is that sometimes I get "value not allowed" validation
> errors from that field. Why? I am restricted to selecting only the
> items in the list given to IS_IN_SE. It seems like it would be
> impossible to select a value that is not allowed.
One thing that occurs to me is that this code is being executed twice, once to
display the form and once to accept it. So if it's possible that
get_last_names() could return different values on successive calls (perhaps
because a user was deleted), you'd see that failure.
(Also, and this isn't relevant to this particular failure, since last names
aren't unique, won't you have a problem elsewhere when there's a duplicate?)
>
> Maybe adding the validator in the controller is not correct but I
> don't know what is the correct way!
>
> Here is a snippet showing the controller and also the table definition
> from the db.py file. (I stripped out the code for other fields just to
> make the snippet shorter. )
>
> Thanks in advance!
>
> =======================
> controller
> =======================
>
> def register_user_custom_form():
>
> # Dynamically get the list of valid last names
> last_names = get_last_names( ) # Call some function to get the
> list
> # Set the validator based on that
> db.reg_test.last_name.requires = IS_IN_SET( last_names, zero =
> "Select a Last Name" )
>
> form=SQLFORM(db.reg_test)
> if form.accepts(request.vars,session):
> response.flash='Your account has been created'
> elif form.errors:
> response.flash='Your account has not been created'
>
> ### and get a list of all users
> records=SQLTABLE(db().select(db.reg_test.ALL))
> return dict(form=form,records=records)
>
> =================
> table definition
> =================
>
> db.define_table('reg_test',
> Field('first_name',required=True,notnull=True,writable=True,
> label="First Name"),
> Field('last_name',required=True,notnull=True,writable=True, label=
> "Last Name"),
> )
> db.reg_test.first_name.requires = IS_NOT_EMPTY()
--
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.