FuelPHPのFieldsetクラスをまとめてみた。1

スポンサーリンク

Fieldsetクラスをまとめてみた。

こんにちは。ファガイです。本日は、Fieldsetクラスについて書きます。(前回くらいにバグに関しては説明したので今日はそれを踏まえた形で書きます。(つまりラジオボタン等のlabelのfor値はバグ修正後です))

また、この内容は、FuelPHPのFormクラスについてまとめてみた。と、FuelPHPのバリデーションクラスについて。を読んでおくと理解しやすいです。

Fieldsetクラス

Fieldsetクラスはフォーム全体をオブジェクト指向的に扱うことが出来るクラスです。出力や、検証も一つのオブジェクトで操作できてしまいます。

基本(Form生成)

ソース

$user_form = Fieldset::forge('user_form');
$user_form->add('name', 'お名前', array('type'=>'text', 'size'=>40, 'value'=>'ここに名前が入ります'));
$user_form->add('sex', '性別', array('type'=>'radio', 'options'=>array(1=>'男性', 2=>'女性'), 'value'=>1));
$user_form->add('submit', '', array('type'=>'submit', 'value'=>'送信'));
echo $user_form->build('member/send');

結果(厳密には結果は違いますが簡易的に)

<form action="http://localhost/test/member/send" accept-charset="utf-8" method="post">
<label for="form_name">お名前</label>
<input type="text" value="ここに名前が入ります" size="40" id="form_name" name="name" />
<label for="form_">性別</label>
<input type="radio" value="1" id="form_sex_0" name="sex" checked="checked" /> <label for="form_sex_0">男性</label><br />
<input type="radio" value="2" id="form_sex_1" name="sex" /> <label for="form_sex_1">女性</label>
<input type="submit" value="送信" id="form_submit" name="submit" />
</form>

このように、Fieldsetクラスではformを簡単に作ることが出来ます。

解説

Fieldset::forge(‘user_form’);

Fieldsetクラスのインスタンスを返すメソッドです。第1引数にはインスタンスに名前を付けることが出来ます。(デフォルトは’default’)
インスタンスに名前をつけるので、このような書き方も可能です。

Fieldset::forge('user_form');
$form = Fieldset::instance('user_form');

これでもOKです。

add()

フィールド(要素)を追加します。

add()メソッド
第1引数:フィールド名(いわゆるname属性,必須)
第2引数:ラベル名
第3引数:オプション。連想配列指定。

ちなみに、第3引数に何もセットしない場合、typeはtextになります。ここは殆どformクラスのinputメソッドと同じような感じですね。

ただし、複数ボックスの場合(selectやradio,checkbox)、第3引数にoptionsというキーを設定し、その中に連想配列で指定する形になります。
また、このような書き方も可能です。

$user_form->field('name')->set_label('名前');
$user_form->field('name')->set_attribute('size', 80);
$user_form->field('name')->set_type('text');

build()

Fieldsetオブジェクトに基づき、フォームの出力をします。

build()メソッド
第1引数:action属性

因みに、第1引数には、コントローラ以下を書くだけで自動的にURLが生成されます。

Fieldsetクラスの検証

まず、検証ルールですが、殆どValidationクラスと同様です。例としては、

$user_form->field('email')
    ->add_rule('required')
    ->add_rule('valid_email');

このような感じです。Validationクラスと異なるのは、addメソッドが無いこと位です。他は同じです。
検証方法は、

if($user_form->validation()->run()) {
    //検証OK
}

このように、Validationクラスと殆ど同じです。バリデーションを通ったフィールドは、$user_form->validated()メソッドで取得が出来ます。連想配列です。

Fieldsetクラスの結果表示の形を変更する

最初の方で書いた結果の部分です。通常、Fieldsetクラスでbuildをすると、tableタグ等が自動的に付与されます。今回は情報を簡略化するために書いていません。
そして、最終的にはFieldsetクラスの表示形式を変えたい場合があると思います。表示形式を変えるためには、まず Core/Config/form.phpAPPPATH/Config/ にコピーしましょう。

form.php

return array(
    'prep_value'            => true,
    'auto_id'               => true,
    'auto_id_prefix'        => 'form_',
    'form_method'           => 'post',
    'form_template'         => "\n\t\t{open}\n\t\t<table>\n{fields}\n\t\t</table>\n\t\t{close}\n",
    'fieldset_template'     => "\n\t\t<tr><td colspan=\"2\">{open}<table>\n{fields}</table></td></tr>\n\t\t{close}\n",
    'field_template'        => "\t\t<tr>\n\t\t\t<td class=\"{error_class}\">{label}{required}</td>\n\t\t\t<td class=\"{error_class}\">{field} <span>{description}</span> {error_msg}</td>\n\t\t</tr>\n",
    'multi_field_template'  => "\t\t<tr>\n\t\t\t<td class=\"{error_class}\">{group_label}{required}</td>\n\t\t\t<td class=\"{error_class}\">{fields}\n\t\t\t\t{field} {label}<br />\n{fields}<span>{description}</span>\t\t\t{error_msg}\n\t\t\t</td>\n\t\t</tr>\n",
    'error_template'        => '<span>{error_msg}</span>',
    'required_mark'         => '*',
    'inline_errors'         => false,
    'error_class'           => 'validation_error',
);

こちらを表にまとめました。主に使わないであろう内容は省いています。

項目名説明
form_methodformのmethod属性の値
form_templateformを作る際のテンプレートです。{open}がformの開始、{fields}がフィールド部分、{close}がformの終了です。こちらはFormクラスで使用されます。
fieldset_template↑のfieldset版。
field_templatefieldのテンプレート。そのままですね。
multi_field_templatefieldが複数だった時のテンプレート。{fields}から{fields}までが項目のループ部分。セレクトボックスはどうやってるのかは不明
error_templateエラーのテンプレート。$user_form->validation()->error()メソッドで使われている模様。
required_mark{required}に対応する値を指定。
inline_errorsエラーを表示するかどうか。
error_classエラー表示に使うクラス名。デフォルトではtdに指定しているのでこのクラスに対してエラーだった時のbackground-color等を指定することになる。

また、初期値の場合、tableタグには何もclass指定がないので、class=”table”を指定しておくと良いと思う。(twitterbootstrap)

このような感じですね。OrmモデルからFieldsetクラスを生成する方法があるのですが、この内容は次回にします。

コメント

タイトルとURLをコピーしました