Extra field in the register form

26 views
Skip to first unread message

Martin Weissenboeck

unread,
May 25, 2015, 4:09:10 AM5/25/15
to web2py-users
I want to have an extra field in the register form but not in the auth_user table.
There has been some threads about this topic in the past.

In user.html I have added:

elif request.args(0)=='register':
    form[0].insert(-1, DIV(
         LABEL(T('registercode'),_class="control-label col-sm-3"),
         DIV(
              INPUT(_name='registercode', _placeholder="z.B. xs5trv", _class="form-control string"),
              SPAN(_class="help-block"),
              _class="col-sm-9"),
         _class="form-group"))
pass
=form

​and in db.py there is a simple validation code:

def validate_registercode(form):
    print "reg", form.vars
    if form.vars.registercode!="123":
        form.errors.registercode="*** Error ***"
       
auth.settings.register_onvalidation=[validate_registercode]

​This creates a nice form containing the new field "registercode". auth calls the validate_registercode, but form.vars does not contain the new field "registercode", only the fields last_name, first_name and so on. What is wrong?

And: is there a better way to add a new field together with its classes?

​R
​egards, Martin​


Massimo Di Pierro

unread,
May 28, 2015, 1:33:09 AM5/28/15
to web...@googlegroups.com, mwei...@gmail.com, mwei...@gmail.com
To add fields to auth forms you have to use

auth.settings.extra_fields['auth_user'] = [ Field(...), Field(...) ]

Martin Weissenboeck

unread,
May 28, 2015, 3:08:12 AM5/28/15
to Massimo Di Pierro, web2py-users
Thank you. But this adds extra fields to the table.
I am look for a way to extend only the registration form and to check the input with an onvalidation function.
Reply all
Reply to author
Forward
0 new messages