表单核心类
API - Core_Form
- Form::open - 创建一个表单
- Form::close - Creates the closing form tag.
- Form::input - Creates a form input. If no type is specified, a "text" type input will
- Form::hidden - Creates a hidden form input.
- Form::password - Creates a password form input.
- Form::file - Creates a file upload form input. No input value can be specified.
- Form::checkbox - Creates a checkbox form input.
- Form::radio - Creates a radio form input.
- Form::textarea - Creates a textarea form input.
- Form::select - Creates a select form input.
- Form::submit - Creates a submit form input.
- Form::image - Creates a image form input.
- Form::button - Creates a button form input. Note that the body of a button is NOT escaped,
- Form::label - Creates a form label. Label text is not automatically translated.
- Form::get_token - 获取一个token的数据数组
- Form::check_token - 校验表单token
- Form::delete_token - 删除相关token,避免被重复利用
创建一个表单
$add_token 参数为是否创建一个token验证隐藏表单,用于预防 CSRF 攻击
// Form will submit back to the current page using POST
echo Form::open();
// Form will submit to 'search' using GET
echo Form::open('search', array('method' => 'get'));
// When "file" inputs are present, you must include the "enctype"
echo Form::open(null, array('enctype' => 'multipart/form-data'));
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$action |
string |
Form action, defaults to the current request URI | null |
$attributes |
array |
Html attributes | null |
$add_token |
boolean |
是否添加token验证功能 | bool true |
string
Creates the closing form tag.
echo Form::close();
string
Creates a form input. If no type is specified, a "text" type input will be returned.
echo Form::input('username', $username);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | null |
$attributes |
array |
Html attributes | null |
string
Creates a hidden form input.
echo Form::hidden('csrf', $token);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | null |
$attributes |
array |
Html attributes | null |
string
Creates a password form input.
echo Form::password('password');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | null |
$attributes |
array |
Html attributes | null |
string
Creates a file upload form input. No input value can be specified.
echo Form::file('image');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$attributes |
array |
Html attributes | null |
string
Creates a checkbox form input.
echo Form::checkbox('remember_me', 1, (bool) $remember);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | null |
$checked |
boolean |
Checked status | bool false |
$attributes |
array |
Html attributes | null |
string
Creates a radio form input.
echo Form::radio('like_cats', 1, $cats);
echo Form::radio('like_cats', 0, !$cats);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | null |
$checked |
boolean |
Checked status | bool false |
$attributes |
array |
Html attributes | null |
string
Creates a textarea form input.
echo Form::textarea('about', $about);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Textarea name | |
$body |
string |
Textarea body | empty |
$attributes |
array |
Html attributes | null |
$double_encode |
boolean |
Encode existing HTML characters | bool false |
string
Creates a select form input.
echo Form::select('country', $countries, $country);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$options |
array |
Available options | null |
$selected |
mixed |
Selected option string, or an array of selected options | null |
$attributes |
array |
Html attributes | null |
string
Creates a submit form input.
echo Form::submit(null, 'Login');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | |
$attributes |
array |
Html attributes | null |
string
Creates a image form input.
echo Form::image(null, null, array('src' => 'media/img/login.png'));
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$value |
string |
Input value | |
$attributes |
array |
Html attributes | null |
string
Creates a button form input. Note that the body of a button is NOT escaped, to allow images and other HTML to be used.
echo Form::button('save', 'Save Profile', array('type' => 'submit'));
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$name |
string |
Input name | |
$body |
string |
Input value | |
$attributes |
array |
Html attributes | null |
string
Creates a form label. Label text is not automatically translated.
echo Form::label('username', 'Username');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$input |
string |
Target input | |
$text |
string |
Label text | null |
$attributes |
array |
Html attributes | null |
string
获取一个token的数据数组
array
校验表单token
当使用 Form::open()
方法开启 token 后,可试用此方法在接受页面中校验token是否正确
bool
删除相关token,避免被重复利用
null
根据一个字符串生成一个token hash
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$str |
string |
$str | |
$key |
unknown |
string