最新の記事や関連記事をお探しの方は下の「サイト内検索」で検索してみてください。
FuelPHPではFieldsetクラスを使うと簡単にフォームのhtmlソースが作れるんです。
その中の各フィールドごとにやるには echo $fieldset->field(‘name’); とかすると出てくるんです。
で、1時間くらいつまずいていたのは、このやり方でフィールドを表示した時にフィールドごとのエラーメッセージを入力フィールドの横にも出せないかと。
フィールドごとのhtmlソースのテンプレートは core/config/form.php のなかで field_template というキーで設定されているんですが、そこにはバッチリ {error_msg} とかかれていれ表示されるはずなんですが、表示されず。
というわけで、GitHub の fuel/core のソースコードを追いかけて classes/fieldset.php やら classes/fieldset/field.php とかを見ていましたが、ふと気づいたのは fielset/field.php のこの一行
| 1 | $error_msg = ($form->get_config('inline_errors') && $this->error()) ? str_replace('{error_msg}', $this->error(), $error_template) : ''; | 
「あれ?もしかして、設定ファイルで inline_errors って値を真にしないとダメってこと?」
はい、設定ファイルを見なおしてみましょう。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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>ntt</tr>¥n",     'error_template'        => '<span>{error_msg}</span>',     'required_mark'         => '*',     'inline_errors'         => false,     'error_class'           => 'validation_error', ); | 
ということで、デフォルトでは false になっているので、フィールドごとのエラー表示はされないというオチでした。
なので、core/config/form.php を app/config/form.php にコピーして inline_errors の値を true に変えてやると問題なくフィールドの横にエラーメッセージが表示されましたとさ。
というか、デフォルトではfalseってのはどうかとおもいますが。。。
コメント
[…] FuelPHPでFieldset_Fieldクラスを使ってフィールド生成するときのエラーメッセージについて | T.T.Soft Code Blog […]