Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Py4web, while creating a database, you can use the form validation feature to check a field against another field. Here is an example of how to do that:

  1. Define your form using the DAL (Database Abstraction Layer) syntax:
db.define_table('users',
    Field('username', requires=[IS_NOT_EMPTY(), IS_NOT_IN_DB(db, 'users.username')]),
    Field('password', 'password'),
    Field('password_confirmation', 'password', label='Confirm Password',
          requires=IS_EQUAL_TO(request.vars.password, error_message='Passwords do not match'))
)
  1. The third field 'password_confirmation' is the field that needs to be checked against the 'password' field. It has a label 'Confirm Password' and requires the input to be a password. The 'requires' parameter contains a validation check that the input must be equal to the value of the 'password' field.

  2. When the form is submitted, Py4web will validate the input and if the two password fields don't match, it will return an error message specified in the 'error_message' parameter.