LoginSignup
10
10

More than 5 years have passed since last update.

Fieldset::forge()で一発設定したい

Last updated at Posted at 2014-04-08

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に渡しやすい感じにも出来た。嬉しい。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10