Web2py Forms & Validators - Web2Py

What is Web2py Forms & Validators?

Web2py comes with effective capabilities for form generation. Four wonderful approaches to construct forms in web2py are as follows −

  • Form − In terms of HTML helpers, it is considered as a low-stage implementation. A form object is aware of its area contents.
  • SQLFORM − It presents the functionalities of Create, replace and Delete to the prevailing database.
  • SQLFORM.manufacturing facility − it's miles considered as abstraction layer on the pinnacle of SQLFORM, which generates a form much like SQLFORM. here, there may be no need to create a brand new database.
  • CRUD methods − as the call suggests, it offers Create, Retrieve, update and Delete features with the similar functionalities based on SQLFORM.


Consider an application, which agrees an input from the customer and has a “submit” button to submit the response.


“default.py” controller will contain the next related function


The associated view "default/display_form.html" will render the display of form in HTML as −

The above example is the normal HTML form, which asks for the user input. The same form can be generated with the helpers like FORM object.


The above function in “default.py” controller includes FORM object (HTML helper) which helps in creation of form.


He form which is generated by the statement {{= form}} serializes the FORM object. When a user fills the form and clicks on the submit button, the form self-submits, and the variable request.vars.value along with its input value is displayed at the bottom.


It helps in creation of a form to the existing database. The steps for its implementation are discussed below.

Establishing connection with database using DAL, this is created using DAL object which is also called DAL constructor. After establishing the connection, user can create the respective table.

Thus, we have created a table named “employee”. The controller builds the form and button with the following statements −

Therefore, for the employee table created, the modification in the controller would be −

There is no modification in View. In the new controller, it is necessary build a FORM, since the SQLFORM constructor built one from the table db.employee is defined in the model. The new form, when serialized, appears as follows –

All tags in the form have names derived from the table and field name.

An SQLFORM object also deals with "upload" fields by means of saving uploaded documents within the "uploads" folder. That is achieved robotically. SQLFORM displays “Boolean” values in the shape of checkboxes and textual content values with the assist of “textures”.

SQLFORM also uses the manner approach. That is vital if the consumer wants to preserve values with an associated SQLFORM.

If form. Process (keep values = true) then it is every day.



from time to time, the user wishes to generate a form in a way that there is an existing database table without the implementation of the database. The user simply wants to take an advantage of the SQLFORM functionality.

that is done via form.factory and it is maintained in a consultation.

The form will appear like SQLFORM with name and image as its fields, but there is no such existing table in database.

The "default/form_from_factory.html" view will represent as −

CRUD methods

CRUD is an API used on top of SQLFORM. as the call suggests, it is used for creation, retrieval, updating and deletion of the appropriate form.

CRUD, in comparison to different APIs in web2py, isn't uncovered; therefore, it is vital that it need to be imported.

The CRUD object defined above provides the following API –


API & Functionality



Returns a list of tables defined in the database.



Returns a create form for thetable tablename.


crud.read(db.tablename, id)

Returns a read-only form fortablenameand record id.


crud.delete(db.tablename, id)

deletes the record


crud.select(db.tablename, query)

Returns a list of records selected from the table.



Returns a tuple (form, records) where form is a search form.



Returns one of the above based on the request.args().

Creation of Form

Let us create a form. Follow the codes given below.


A new model is created under the models folder of the application. The name of the file would be “dynamic_search.py”.


The associated file namely “dynamic_search.py” under controllers section will include the following code −


We can render this with the following view.

Here is what it looks like –


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Web2Py Topics