​

A proposal to extend the register form and the login form

The issue:

I want to have a register form with an extra field,* which should not be
part of auth_user*. The purpose: a new user can only register if this field
contrains (e.g.) some special code. This code can be checked by a
validation function, set by auth.settings.register_onvalidation

I have tried to change the form which is created by def user in default.py.
The result was a very long code which is hard to read and which needs some
additional statements for the error message. A better way would be a small
modification of gluon/tools.py. And the same could be done with the
login-form.

We need only 6 additional lines in gluon/tools.py

At the end of default_settings (line 1206) add:

​        ​
register_extra_fields=[],

​      ​
​  ​
login_extra_fields=[]


At the end of settings.update (line 1506) add:

​        ​
register_extra_fields = [],

​    lo​
gin_extra_fields = []


​I​
n def login() add after line 2561ff:


           if settings.remember_me_form:

               extra_fields = [

                   Field('remember_me', 'boolean', default=False,

                         label = self.messages.label_remember_me)]

           else:

               extra_fields = []

           extra_fields += self.settings.login_extra_fields


In def register() add after line 2862 ff:

      if self.settings.register_verify_password:

           extra_fields = [

               Field("password_two", "password", requires=IS_EQUAL_TO(

                       request.post_vars.get(passfield, None),

                       error_message=self.messages.mismatched_password),

                       label=current.T("Confirm Password"))]

       else:

                      extra_fields = []

       extra_fields += self.settings.register_extra_fields

​How to use:​

Yes, it’s only a very simple check: the user has to write “VALID” into the
field “extracode”.

In db.py, before auth.define_tables:

def validate_registercode(form):

    if form.vars.extracode != "VALID":

          form.errors.extracode=T("Invalid extra code")

auth.settings.register_extra_fields=

   [Field('extracode',

       label='My extracode',

       comment='This is the extracode')

   ]

auth.settings.register_onvalidation=[validate_registercode]

That’s all. Now we have a nice register form with an extra field labeled “My
extracode”. If the use
​r
 does not know the right answer “VALID” he could not register. All css
attributes are the same as in the remaining register form.


​
​Regards, Martin​

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to