0

I have made a 'user_details' table whose 'user_id' field I have referenced to default 'auth_user' table. Now after registration, I show the user his profile and ask him to complete it. But, the problem is that how can I check if the profile is complete or not after the user submits the updated profile.

Here is my 'user_details' table ,

db.define_table('user_details',
    Field('user_id'),
    Field('gender',requires=IS_IN_SET(['Male','Female','Other'])),
    Field('phone_number',requires=IS_MATCH('\d{10}')),
    Field('preferred_mess'),
    Field('preferred_caterer'),
    Field('state_name','string'),
    Field('account_initial_balance'),
    Field('account_curr_balance'),
    Field('your_booking_preference',requires=IS_IN_SET(['''Don't Book Food Automatically''','Book Food Automatically'])))

1 Answers1

0

You can add onvalidation function to check whether user filled all fields or not.

Read - onvalidation

def form_validation():
    if not form.vars.gender:
        form.errors.gender = "Value not allowed"
    if not form.vars.phone_number:
        form.errors.phone_number = "Value not allowed"
    if not form.vars.preferred_mess:
        form.errors.preferred_mess = "Value not allowed"
    if not form.vars.preferred_caterer:
        form.errors.preferred_caterer = "Value not allowed"
    if not form.vars.state_name:
        form.errors.state_name = "Value not allowed"
    if not form.vars.account_initial_balance:
        form.errors.account_initial_balance = "Value not allowed"
    if not form.vars.account_curr_balance:
        form.errors.account_curr_balance = "Value not allowed"
    if not form.vars.your_booking_preference:
        form.errors.your_booking_preference = "Value not allowed"

Or make fields required=True

db.define_table('user_details',
    Field('user_id', required=True),
    Field('gender',requires=IS_IN_SET(['Male','Female','Other']), required=True),
    Field('phone_number',requires=IS_MATCH('\d{10}'), required=True),
    Field('preferred_mess', required=True),
    Field('preferred_caterer', required=True),
    Field('state_name','string', required=True),
    Field('account_initial_balance', required=True),
    Field('account_curr_balance', required=True),
    Field('your_booking_preference',requires=IS_IN_SET(['''Don't Book Food Automatically''','Book Food Automatically']), required=True))

You can also do it using javascript.

Gaurav Vichare
  • 1,143
  • 2
  • 11
  • 26
  • Thanks for your response. But, I think using 'required' will pose problem when I will insert a row in database with field values as 'None' except 'id' field. As some times I have created a new row in table without initializing it with valid values. That is why I think that your first solution is more relevant to my context. Thank you. – Upendra Kumar Oct 23 '15 at 09:39
  • The `required` parameter forces it on web2py input, and not on the database level. If you do apply `notnull=True` [as a parameter](http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-constructor), you should still be able to insert data. Not [all data input](http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#validate_and_insert--validate_and_update) is validated with the web2py schema. – Remco Oct 23 '15 at 23:55