Form 类別

Form 类別可以被用來建立单獨的表单元素,或建立一个有驗證的完整表单, 後者則是結合了 Fieldset 类別。

建立单獨的表单元素

open($attributes = array(), $hidden = array())

建立一个表单開始标籤。

靜態
參数
參数 預設 描述
$attributes
array()
一个做为 action 屬性的字串或一个設定的陣列, 如果沒給,action 将使用目前 Uri, 屬性的值将被用作 HTML 标籤特性。
$hidden
array()
一个欄位名稱和值的關聯陣列, 将全部被設定为隱藏欄位。
回傳 字串
範例
// 回傳 <form action="http://mydomain.com/uri/to/form" accept-charset="utf-8" method="post">
echo Form::open('uri/to/form');

// 回傳 <form action="http://google.com/" accept-charset="utf-8" method="get">
echo Form::open(array('action' => 'http://google.com/', 'method' => 'get'));

close()

建立一个表单結束标籤。

靜態
參数 (無)
回傳 字串
範例
// 回傳 </form>
echo Form::close();

input($field, $value = null, $attributes = array())

建立一个 html input 元素,它可以設定使用的欄位名稱、值和标籤的屬性, 或合为一个陣列做为第一个參数。

靜態
參数
參数 預設 描述
$field 必要 欄位名稱的字串或标籤屬性的陣列。
$value
null
欄位值,當第一个參数是陣列時将被忽略。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::input('name', 'value', array('style' => 'border: 2px;'));
// 更多可以添加

button($field, $value = null, $attributes = array())

建立一个 html button 元素,它可以設定使用的欄位名稱、值和标籤的屬性, 或合为一个陣列做为第一个參数。

靜態
參数
參数 預設 描述
$field 必要 欄位名稱的字串或标籤屬性的陣列。
$value
null
欄位值,當第一个參数是陣列時将被忽略。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::button('name', 'value', array('style' => 'border: 2px;'));

hidden($field, $value = null, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'hidden'

csrf()

建立一个 HTML input 元素,帶有自动設为產生的 CSRF 符記的 value 屬性。

password($field, $value = null, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'password'

radio($field, $value = null, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'radio'
为了向下相容,此方法也接受:radio($field, $value = null, array $attributes = array())

靜態
參数
參数 預設 描述
$field 必要 欄位名稱的字串或标籤屬性的陣列。
$value
null
欄位值,當第一个參数是陣列時将被忽略。
$checked
null
是否選取(如果是布林)或符合 $value (如果是字串), 當第一个參数是陣列時将被忽略。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::label('Male', 'gender');
echo Form::radio('gender', 'Male', true);
echo Form::label('Female', 'gender');
echo Form::radio('gender', 'Female');

checkbox($field, $value = null, $checked = null, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'checkbox'
为了向下相容,此方法也接受:checkbox($field, $value = null, array $attributes = array())

靜態
參数
參数 預設 描述
$field 必要 欄位名稱的字串或标籤屬性的陣列。
$value
null
欄位值,當第一个參数是陣列時将被忽略。
$checked
null
是否選取(如果是布林)或符合 $value (如果是字串), 當第一个參数是陣列時将被忽略。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::label('Male', 'gender');
echo Form::checkbox('gender', 'Male', true);
echo Form::label('Female', 'gender');
echo Form::checkbox('gender', 'Female');

file($field, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'file'

reset($field, $value = null, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'reset'

submit($field, $value = null, $attributes = array())

这是一个 Form::input() 的別名,它会自动設定 type 屬性为 'submit'

textarea($field, $value = null, $attributes = array())

建立一个 html textarea 元素,它可以設定使用的欄位名稱、值和标籤的屬性, 或合为一个陣列做为第一个參数。

靜態
參数
參数 預設 描述
$field 必要 欄位名稱的字串或标籤屬性的陣列。
$value
null
欄位值,當第一个參数是陣列時将被忽略。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::textarea('description', 'enter here', array('rows' => 6, 'cols' => 8));
// 更多可以添加

select($field, $values = null, $options = array(), $attributes = array())

建立一个 html select 元素,它可以設定使用的欄位名稱、已選擇的值、選項和标籤的屬性, 或合为一个陣列做为第一个參数。

靜態
參数
參数 預設 描述
$field 必要 欄位名稱的字串或标籤屬性的陣列。
$values
null
選擇的值或多選值的陣列, 當第一个參数是陣列時将被忽略。
$options
array()
value=>label 配對的關聯陣列,也可能包含 option 群組如 opt_name=>array(),該陣列包含自己的一組 value=>label 配對。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::select('country', 'none', array(
	'none' => 'None',
	'europe' => array(
		'uk' => 'United Kingdom',
		'nl' => 'Netherlands'
	),
	'us' => 'United States'
));
// 更多可以添加

label($label, $id = null, $attributes = array())

建立一个 html label 元素,它可以設定使用的标籤、連結的 id 和标籤的屬性, 或合为一个陣列做为第一个參数。

靜態
參数
參数 預設 描述
$label 必要 标籤的字串或标籤屬性的陣列。
$id
null
此标籤屬於的欄位 id。
$attributes
array()
这些将被用作 HTML 标籤特性。
回傳 字串
範例
echo Form::label('Username', 'username');
// 更多可以添加

fieldset_open($attributes = array(), $legend = null)

建立一个欄位集開始标籤。

靜態
參数
參数 預設 描述
$attributes
array()
一个設定的陣列;可能使用 'legend' 鍵去設定欄位集的 legend 屬性。 屬性的值将被用作 HTML 标籤特性。
$legend
字串
要用作 legend 欄位集選項的字串
回傳 字串
範例
// 回傳 <fieldset >
echo Form::fieldset_open();

// 回傳 <fieldset class="example-class" id="example-id">
echo Form::fieldset_open(array('class' => 'example-class', 'id' => 'example-id'));

// 回傳 <fieldset class="example-class" id="example-id"><legend>Custom Legend</legend>
echo Form::fieldset_open(array('class' => 'example-class', 'id' => 'example-id'), 'Custom Legend');

// 回傳 <fieldset class="example-class" id="example-id"><legend>Custom Legend</legend>
echo Form::fieldset_open(array('class' => 'example-class', 'id' => 'example-id', 'legend' => 'Custom Legend'));

fieldset_close()

建立一个欄位集結束标籤。

靜態
參数 (無)
回傳 字串
範例
// 回傳 </fieldset>
echo Form::fieldset_close();

以 OOP 方式使用欄位集建立表单

詳見 Fieldset