# 14.3 Forms In web development, the following workflow will probably look quite familiar: - Open a web page showing a form - Users fill out and submit the form - If a user submits some invalid information or has neglected to fill out a required field, the form will be returned to the user (along with the filled in data) with some descriptive information about the problem. - Users re-fill the invalid fields and continue attempting to submit the form until it's accepted At the receiving end, the script must: - Check the user submitted form data. - Verify whether the data is the correct type and of the appropriate standard. For example, if a username is submitted, it must verify that it contains only valid characters. Other examples would be checking for minimum and maximum lengths, username uniqueness, and so on. - Filtering data and cleaning up unsafe characters to guarantee that our application only processes data which is safe. - If necessary, pre-format the data (or data gaps need to be cleared through the HTML coding and so on.) - Prepare the data for insertion into the database Although the procedure is not very complex, it usually requires a lot of boilerplate. In addition, web applications often use a variety of different control structures to display error messages on returned pages. Implementing form validation is a simple but boring task. ## Forms and validation For developers, the general development process can be quite complex, but it's mostly repetitive work. Suppose a scenario arises where you suddenly need to add a form to your project, causing you to rewrite all of the local code tied in with the form. We know that `structs` are a very commonly used data structure in Go, and Beego uses them to its advantage for processing form information. First, we define a `struct` with fields corresponding to the fields in our form element. We can use `struct` tags which map to the form element, as shown below: When developing Web applications, first define a struct that matches a field to a corresponding form element, defined by using a struct tag corresponding to the element information and authentication information, as shown below: For developers, the general development process is very complex, and mostly consists of repeating the same work process. Assuming a scenario for a project whereby a need arises to add data to a form, then the local code of the entire process needs to be modified. We know in Go a struct is a common data structure, so beego uses a form struct to process form information. First define a `struct` with fields corresponding to our form element, using `struct` tags to define the corresponding form element and authentication information, like so: type User struct{ Username string `form:text,valid:required` Nickname string `form:text,valid:required` Age int `form:text,valid:required|numeric` Email string `form:text,valid:required|valid_email` Introduce string `form:textarea` } After defining our `struct`, we can add this action in our controller: func (this *AddController) Get() { this.Data["form"] = beego.Form(&User{}) this.Layout = "admin/layout.html" this.TplNames = "admin/add.tpl" } The form is displayed in our template like so:

New Blog Post

{{.form.render()}}
Above, we've defined the entire first step of displaying a form mapped to a `struct`. The next step is for users to fill in their information and submit the form, after which the server will receive the data and verify it. Finally, the record will be inserted into the database. func (this *AddController) Post() { var user User form := this.GetInput(&user) if !form.Validates() { return } models.UserInsert(&user) this.Ctx.Redirect(302, "/admin/index") } ## Form type The following table lists the corresponding form element information:
Name parameter Description
text No textbox input box
button No button
checkbox No multi-select box
dropdown No drop-down selection box
file No file upload
hidden No hidden elements
password No password input box
radio No single box
textarea No text input box
## Form validation The following table lists some form validation rules native to Beego that can be used:
rules parameter Description Example
required No If the element is empty, it returns FALSE
matches Yes if the form element's value with the corresponding form field parameter values are not equal, then return FALSE matches [form_item]
is_unique Yes if the form element's value with the specified field in a table has duplicate data, it returns False( Translator's Note: For example is_unique [User.Email], then the validation class will look for the User table in the Email field there is no form elements with the same value, such as deposit repeat, it returns false, so developers do not have to write another Callback verification code.) is_unique [table.field]
min_length Yes form element values if the character length is less than the number of defined parameters, it returns FALSE min_length [6]
max_length Yes if the form element's value is greater than the length of the character defined numeric argument, it returns FALSE max_length [12]
exact_length Yes if the form element values and parameters defined character length number does not match, it returns FALSE exact_length [8]
greater_than Yes If the form element values non- numeric types, or less than the value defined parameters, it returns FALSE greater_than [8]
less_than Yes If the form element values non- numeric types, or greater than the value defined parameters, it returns FALSE less_than [8]
alpha No If the form element value contains characters other than letters besides, it returns FALSE
alpha_numeric No If the form element values contained in addition to letters and other characters other than numbers, it returns FALSE
alpha_dash No If the form element value contains in addition to the letter/ number/ underline/ characters other than dash, returns FALSE
numeric No If the form element value contains characters other than numbers in addition, it returns FALSE
integer No except if the form element contains characters other than an integer, it returns FALSE
decimal Yes If the form element type( non- decimal ) is not complete, it returns FALSE
is_natural No value if the form element contains a number of other unnatural values ( other values excluding zero ), it returns FALSE. Natural numbers like this: 0,1,2,3.... and so on.
is_natural_no_zero No value if the form element contains a number of other unnatural values ( other values including zero ), it returns FALSE. Nonzero natural numbers: 1,2,3..... and so on.
valid_email No If the form element value contains invalid email address, it returns FALSE
valid_emails No form element values if any one value contains invalid email address( addresses separated by commas in English ), it returns FALSE.
valid_ip No if the form element's value is not a valid IP address, it returns FALSE.
valid_base64 No if the form element's value contains the base64-encoded characters in addition to other than the characters, returns FALSE.
## Links - [Directory](preface.md) - Previous section: [Sessions](14.2.md) - Next section: [User validation](14.4.md)