Merging other languages

This commit is contained in:
James Miranda
2016-09-23 18:01:10 -03:00
parent 380a8ee74c
commit de3c5bdaa4
490 changed files with 24539 additions and 24588 deletions

View File

@@ -1,281 +1,350 @@
# 14.3 表单及验证支持
在Web开发中对于这样的一个流程可能很眼熟
- 打开一个网页显示出表单。
- 用户填写并提交了表单。
- 如果用户提交了一些无效的信息,或者可能漏掉了一个必填项,表单将会连同用户的数据和错误问题的描述信息返回。
- 用户再次填写,继续上一步过程,直到提交了一个有效的表单。
在接收端,脚本必须:
- 检查用户递交的表单数据。
- 验证数据是否为正确的类型,合适的标准。例如,如果一个用户名被提交,它必须被验证是否只包含了允许的字符。它必须有一个最小长度,不能超过最大长度。用户名不能与已存在的他人用户名重复,甚至是一个保留字等。
- 过滤数据并清理不安全字符,保证逻辑处理中接收的数据是安全的。
- 如果需要预格式化数据数据需要清除空白或者经过HTML编码等等。
- 准备好数据,插入数据库。
尽管上面的过程并不是很复杂,但是通常情况下需要编写很多代码,而且为了显示错误信息,在网页中经常要使用多种不同的控制结构。创建表单验证虽简单,实施起来实在枯燥无味。
## 表单和验证
对于开发者来说一般开发过程都是相当复杂而且大多是在重复一样的工作。假设一个场景项目中忽然需要增加一个表单数据那么局部代码的整个流程都需要修改。我们知道Go里面struct是常用的一个数据结构因此beego的form采用了struct来处理表单信息。
首先定义一个开发Web应用时相对应的struct一个字段对应一个form元素通过struct的tag来定义相应的元素信息和验证信息如下所示
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之后接下来在controller中这样操作
func (this *AddController) Get() {
this.Data["form"] = beego.Form(&User{})
this.Layout = "admin/layout.html"
this.TplNames = "admin/add.tpl"
}
在模板中这样显示表单
<h1>New Blog Post</h1>
<form action="" method="post">
{{.form.render()}}
</form>
上面我们定义好了整个的第一步从struct到显示表单的过程接下来就是用户填写信息服务器端接收数据然后验证最后插入数据库。
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元素信息
<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
<tbody><tr>
<th>名称</th>
<th>参数</th>
<th>功能描述</th>
</tr>
<tr>
<td class="td"><strong>text</strong></td>
<td class="td">No</td>
<td class="td">textbox输入框</td>
</tr>
<tr>
<td class="td"><strong>button</strong></td>
<td class="td">No</td>
<td class="td">按钮</td>
</tr>
<tr>
<td class="td"><strong>checkbox</strong></td>
<td class="td">No</td>
<td class="td">多选择框</td>
</tr>
<tr>
<td class="td"><strong>dropdown</strong></td>
<td class="td">No</td>
<td class="td">下拉选择框</td>
</tr>
<tr>
<td class="td"><strong>file</strong></td>
<td class="td">No</td>
<td class="td">文件上传</td>
</tr>
<tr>
<td class="td"><strong>hidden</strong></td>
<td class="td">No</td>
<td class="td">隐藏元素</td>
</tr>
<tr>
<td class="td"><strong>password</strong></td>
<td class="td">No</td>
<td class="td">密码输入框</td>
</tr>
<tr>
<td class="td"><strong>radio</strong></td>
<td class="td">No</td>
<td class="td">单选框</td>
</tr>
<tr>
<td class="td"><strong>textarea</strong></td>
<td class="td">No</td>
<td class="td">文本输入框</td>
</tr>
</tbody></table>
## 表单验证
以下列表将列出可被使用的原生规则
<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
<tbody><tr>
<th>规则</th>
<th>参数</th>
<th>描述</th>
<th>举例</th>
</tr>
<tr>
<td class="td"><strong>required</strong></td>
<td class="td">No</td>
<td class="td">如果元素为空则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>matches</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素的值与参数中对应的表单字段的值不相等则返回FALSE</td>
<td class="td">matches[form_item]</td>
</tr>
<tr>
<td class="td"><strong>is_unique</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素的值与指定数据表栏位有重复则返回False译者注比如is_unique[User.Email]那么验证类会去查找User表中Email栏位有没有与表单元素一样的值如存重复则返回false这样开发者就不必另写Callback验证代码。</td>
<td class="td">is_unique[table.field]</td>
</tr>
<tr>
<td class="td"><strong>min_length</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素值的字符长度少于参数中定义的数字则返回FALSE</td>
<td class="td">min_length[6]</td>
</tr>
<tr>
<td class="td"><strong>max_length</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素值的字符长度大于参数中定义的数字则返回FALSE</td>
<td class="td">max_length[12]</td>
</tr>
<tr>
<td class="td"><strong>exact_length</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素值的字符长度与参数中定义的数字不符则返回FALSE</td>
<td class="td">exact_length[8]</td>
</tr>
<tr>
<td class="td"><strong>greater_than</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素值是非数字类型或小于参数定义的值则返回FALSE</td>
<td class="td">greater_than[8]</td>
</tr>
<tr>
<td class="td"><strong>less_than</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素值是非数字类型或大于参数定义的值则返回FALSE</td>
<td class="td">less_than[8]</td>
</tr>
<tr>
<td class="td"><strong>alpha</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值中包含除字母以外的其他字符则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>alpha_numeric</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值中包含除字母和数字以外的其他字符则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>alpha_dash</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值中包含除字母/数字/下划线/破折号以外的其他字符则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>numeric</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值中包含除数字以外的字符,则返回 FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>integer</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素中包含除整数以外的字符则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>decimal</strong></td>
<td class="td">Yes</td>
<td class="td">如果表单元素中输入非小数不完整的值则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>is_natural</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值中包含了非自然数的其他数值 其他数值不包括零则返回FALSE。自然数形如0,1,2,3....等等。</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>is_natural_no_zero</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值包含了非自然数的其他数值 其他数值包括零则返回FALSE。非零的自然数1,2,3.....等等。</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>valid_email</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值包含不合法的email地址则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>valid_emails</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素值中任何一个值包含不合法的email地址地址之间用英文逗号分割则返回FALSE</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>valid_ip</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素的值不是一个合法的IP地址则返回FALSE。</td>
<td class="td">&nbsp;</td>
</tr>
<tr>
<td class="td"><strong>valid_base64</strong></td>
<td class="td">No</td>
<td class="td">如果表单元素的值包含除了base64 编码字符之外的其他字符则返回FALSE。</td>
<td class="td">&nbsp;</td>
</tr>
</tbody></table>
## links
* [目录](<preface.md>)
* 上一节: [Session支持](<14.2.md>)
* 下一节: [用户认证](<14.4.md>)
# 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
<h1>New Blog Post</h1>
<form action="" method="post">
{{.form.render()}}
</form>
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:
<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
<tbody>
<tr>
<th>Name</th>
<th>parameter</th>
<th>Description</th>
</tr>
<tr>
<td class="td"><strong>text</strong>
</td>
<td class="td">No</td>
<td class="td">textbox input box</td>
</tr>
<tr>
<td class="td"><strong>button</strong>
</td>
<td class="td">No</td>
<td class="td">button</td>
</tr>
<tr>
<td class="td"><strong>checkbox</strong>
</td>
<td class="td">No</td>
<td class="td">multi-select box</td>
</tr>
<tr>
<td class="td"><strong>dropdown</strong>
</td>
<td class="td">No</td>
<td class="td">drop-down selection box</td>
</tr>
<tr>
<td class="td"><strong>file</strong>
</td>
<td class="td">No</td>
<td class="td">file upload</td>
</tr>
<tr>
<td class="td"><strong>hidden</strong>
</td>
<td class="td">No</td>
<td class="td">hidden elements</td>
</tr>
<tr>
<td class="td"><strong>password</strong>
</td>
<td class="td">No</td>
<td class="td">password input box</td>
</tr>
<tr>
<td class="td"><strong>radio</strong>
</td>
<td class="td">No</td>
<td class="td">single box</td>
</tr>
<tr>
<td class="td"><strong>textarea</strong>
</td>
<td class="td">No</td>
<td class="td">text input box</td>
</tr>
</tbody>
</table>
## Forms authentication
The following list may be used are listed rules native:
<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
<tbody>
<tr>
<th>rules</th>
<th>parameter</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td class="td"><strong>required</strong>
</td>
<td class="td">No</td>
<td class="td">If the element is empty, it returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>matches</strong>
</td>
<td class="td">Yes</td>
<td class="td">if the form element's value with the corresponding form field parameter values are not equal, then return
FALSE</td>
<td class="td">matches [form_item]</td>
</tr>
<tr>
<td class="td"><strong>is_unique</strong>
</td>
<td class="td">Yes</td>
<td class="td">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.)</td>
<td class="td">is_unique [table.field]</td>
</tr>
<tr>
<td class="td"><strong>min_length</strong>
</td>
<td class="td">Yes</td>
<td class="td">form element values if the character length is less than the number defined parameters, it returns FALSE</td>
<td class="td">min_length [6]</td>
</tr>
<tr>
<td class="td"><strong>max_length</strong>
</td>
<td class="td">Yes</td>
<td class="td">if the form element's value is greater than the length of the character defined numeric argument, it returns
FALSE</td>
<td class="td">max_length [12]</td>
</tr>
<tr>
<td class="td"><strong>exact_length</strong>
</td>
<td class="td">Yes</td>
<td class="td">if the form element values and parameters defined character length number does not match, it returns FALSE</td>
<td class="td">exact_length [8]</td>
</tr>
<tr>
<td class="td"><strong>greater_than</strong>
</td>
<td class="td">Yes</td>
<td class="td">If the form element values non- numeric types, or less than the value defined parameters, it returns FALSE</td>
<td class="td">greater_than [8]</td>
</tr>
<tr>
<td class="td"><strong>less_than</strong>
</td>
<td class="td">Yes</td>
<td class="td">If the form element values non- numeric types, or greater than the value defined parameters, it returns FALSE</td>
<td class="td">less_than [8]</td>
</tr>
<tr>
<td class="td"><strong>alpha</strong>
</td>
<td class="td">No</td>
<td class="td">If the form element value contains characters other than letters besides, it returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>alpha_numeric</strong>
</td>
<td class="td">No</td>
<td class="td">If the form element values contained in addition to letters and other characters other than numbers, it returns
FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>alpha_dash</strong>
</td>
<td class="td">No</td>
<td class="td">If the form element value contains in addition to the letter/ number/ underline/ characters other than dash,
returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>numeric</strong>
</td>
<td class="td">No</td>
<td class="td">If the form element value contains characters other than numbers in addition, it returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>integer</strong>
</td>
<td class="td">No</td>
<td class="td">except if the form element contains characters other than an integer, it returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>decimal</strong>
</td>
<td class="td">Yes</td>
<td class="td">If the form element type( non- decimal ) is not complete, it returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>is_natural</strong>
</td>
<td class="td">No</td>
<td class="td">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.</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>is_natural_no_zero</strong>
</td>
<td class="td">No</td>
<td class="td">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.</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>valid_email</strong>
</td>
<td class="td">No</td>
<td class="td">If the form element value contains invalid email address, it returns FALSE</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>valid_emails</strong>
</td>
<td class="td">No</td>
<td class="td">form element values if any one value contains invalid email address( addresses separated by commas in English
), it returns FALSE.</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>valid_ip</strong>
</td>
<td class="td">No</td>
<td class="td">if the form element's value is not a valid IP address, it returns FALSE.</td>
<td class="td"></td>
</tr>
<tr>
<td class="td"><strong>valid_base64</strong>
</td>
<td class="td">No</td>
<td class="td">if the form element's value contains the base64-encoded characters in addition to other than the characters,
returns FALSE.</td>
<td class="td"></td>
</tr>
</tbody>
</table>
## Links
- [Directory](preface.md)
- Previous section: [Session](14.2.md)
- Next section: [User validation](14.4.md)