# 14.3 Form In Web Development For such a process may be very familiar: - Open a web page showing the form. - Users fill out and submit the form. - If a user submits some invalid information, or you might have missed a required item, the form will be together with the user's data and the error description of the problem to return. - Users fill in again to continue the previous step process until the submission of a valid form. At the receiving end, the script must: - Check the user submitted form data. - Verify whether the data is the correct type, the appropriate standard. For example, if a user name is submitted, it must be verified whether or contains only characters allowed. It must have a minimum length can not exceed the maximum length. User name can not already exist with others duplicate user name, or even a reserved word and so on. - Filtering data and clean up the unsafe character that guarantees a logic processing received data is safe. - If necessary, pre-formatted data( or data gaps need to be cleared through the HTML coding and so on. ) - Preparing the data into the database. While the above process is not very complex, but usually need to write a lot of code, and in order to display an error message on the page often use a variety of different control structures. Create a form validation, although simple to implement it boring. ## Forms and validation For developers, the general development process is very complex, and mostly are repeating the same work. Assuming a scenario project suddenly need to add a form data, then the local code of the entire process needs to be modified. We know that Go inside a struct is a common data structure, so beego the form struct used to process form information. First define a developing Web applications corresponding struct, a field corresponds to a form element, through the struct tag to define the corresponding element information and authentication information, as follows: 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` } Struct defined in this way after the next operation in the controller func (this *AddController) Get() { this.Data["form"] = beego.Form(&User{}) this.Layout = "admin/layout.html" this.TplNames = "admin/add.tpl" } This form is displayed in the template

New Blog Post

{{.form.render()}}
Above we defined the entire first step to display the form from the struct process, the next step is the user fill out the information, and then verify that the server receives data, and finally 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 list to 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
## Forms authentication The following list may be used are listed rules native:
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 have 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 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: [Session](14.2.md) - Next section: [User validation](14.4.md)