How to process a custom register form?

308 views
Skip to first unread message

Apple Mason

unread,
Sep 6, 2013, 5:17:23 PM9/6/13
to web...@googlegroups.com
The default register form has too many fields, so I created a slimmed-down custom one:

register.html:

<div>
Username: {{=form.custom.widget.username}}
Email: {{=form.custom.widget.email}}
Password: {{=form.custom.widget.password}}

{{=form.custom.submit}}
</div>

default.py:

def register():
     
return dict(form=auth.register())

def login():
     
return dict(form=auth.login())

The default validation works if you submit an invalid input (ie, an invalid email will error on the form). But if the information is correct, the information doesn't get saved into the database, so the user is never created.

I thought maybe doing something like:

form = auth.register()
if form.process().accepted:
   
# Then what??

But that seems incorrect because it looks like it'll process the form twice.

What's the proper way of handling user actions on custom forms? ie, login, registration, request_reset_password...

Anthony

unread,
Sep 6, 2013, 5:38:55 PM9/6/13
to
Did you also include form.custom.begin and form.custom.end (you need the latter, or the _formkey check will fail silently)? Anyway, if you just want to exclude some fields from the register form, you can set the readable and writable attributes to False within the user function:

def user():
   
if request.args(0) == 'register':
       
for field in [list, of, fields]:
            db
.auth_user[field].readable = db.auth_user[field].writable = False
   
return dict(form=auth())


Anthony

Apple Mason

unread,
Sep 6, 2013, 7:51:54 PM9/6/13
to web...@googlegroups.com
Oops, I do have form.custom.begin and form.custom.end. I had forgotten to type it here.

I also wanted a custom view for the registration, login, and whatever else, so my understanding is that I can do this by using form=auth.register() and form=auth.login(). Then in their respective views, I can format the html whatever way I want with form.custom.begin and form.custom.end.

Is form=auth.register() supposed to handle inserting the new user to the database? I've only dealt with handling it in the controller with 'if form.process().accepted', so looking at 'return(form=auth.register()) is a bit confusing.



On Friday, September 6, 2013 5:38:15 PM UTC-4, Anthony wrote:
Did you also include form.custom.begin and form.custom.end (you need the latter, or the _formkey check will fail silently)? Anyway, if you just want to exclude some fields from the register form, you can set the readable and writable attributes to False within the user function:

def user():
   
if request.args(0) == 'register':
       
for field in [list, of, fields]:
            db
.auth_user[field].readable = db.auth_user[field].writable = False
   
return dict(form=auth())


Anthony

On Friday, September 6, 2013 5:17:23 PM UTC-4, Apple Mason wrote:

Anthony

unread,
Sep 6, 2013, 8:55:13 PM9/6/13
to web...@googlegroups.com
On Friday, September 6, 2013 7:51:54 PM UTC-4, Apple Mason wrote:
Oops, I do have form.custom.begin and form.custom.end. I had forgotten to type it here.

I also wanted a custom view for the registration, login, and whatever else, so my understanding is that I can do this by using form=auth.register() and form=auth.login(). Then in their respective views, I can format the html whatever way I want with form.custom.begin and form.custom.end.

You can also create a single view and include logic like this:

{{if request.args(0) == 'login':}}
[code to show login form]
{{elif request.args(0) == 'register':}}
[code to show register form]
{{else:}}
{{=form  # use defaults for other forms}}
{{pass}}

Is form=auth.register() supposed to handle inserting the new user to the database? I've only dealt with handling it in the controller with 'if form.process().accepted', so looking at 'return(form=auth.register()) is a bit confusing.

Don't call form.process() with an Auth form -- the Auth method already calls .process(), and calling it twice will lead to errors. Yes, auth.register() does handle inserting the user into the database.

Anthony

Apple Mason

unread,
Sep 8, 2013, 2:34:40 AM9/8/13
to web...@googlegroups.com
Thanks, I think I understand now. The last snippet looks very clean as opposed to defining the various auth actions in the controller.

Also, I think I figured out why the form isn't saving. It's because there are errors on the form, but the errors are hidden. Right now I only display three fields: username, email, and password. But because auth_user requires first_name and last_name not be empty(Error: "
Cannot be empty")
, the submission actually fails since they are empty. And since there's no html code for the extraneous fields, the error never shows.

From your first post you suggest:


def user():
   
if request.args(0) == 'register':
       
for field in [list, of, fields]:
            db
.auth_user[field].readable = db.auth_user[field].writable = False

    return dict(form=auth())

But won't that set the db.auth_user[field] readable and writable to False every time a user visits the register page?

Is there any difference doing it this way inside db.py (using the 'last_name' field) instead of in the controller:

db.auth_user['last_name'].readable = db.auth_user['last_name'].writable = False
Reply all
Reply to author
Forward
0 new messages