FuelPHPのFieldsetは便利だが、forgeした後に色々と設定していくため、Controllerが煩雑になる。
fuel/app/classes/controller/foo.php
$foo_fieldset = Fieldset::forge('foo');
$foo_fieldset->add(
'article_title',
'Title',
array('class' => 'pretty_input')
);
$ops = array('male', 'female');
$foo_fieldset->add(
'gender',
'',
array('options' => $ops, 'type' => 'radio', 'value' => 'true')
);
$vm = ViewModel::forge('forge');
$vm->set('fs', $foo_fieldset);
↑みたいなのは嫌だ。
目指すところ
$foo_fieldset = Foo_Fieldset::forge();
$vm = ViewModel::forge('foo');
$vm->set('fs', $foo_fieldset);
↑みたいな感じが俺的には嬉しい。
fuel/app/classes/fieldset/foo.php
class Fieldset_Foo extends \Fuel\Core\Fieldset
{
public static function forge($name = 'foo', array $config = array())
{
if((static::$_instance = parent::forge($name, $config)) === false){
return false;
}
static::$_instance->add(
'article_title',
'Title',
array('class' => 'pretty_input')
);
$ops = array('male', 'female');
static::$_instance->add(
'gender',
'',
array('options' => $ops, 'type' => 'radio', 'value' => 'true')
);
return static::$_instance;
}
}
更に基底クラスを用意して、mustacheに渡しやすい感じにも出来た。嬉しい。