Fieldset
私は2つの非常によく似たMyFooFieldset
とを持っていMyBarFieldset
ます。コードの重複を避けるために、 を作成し、コード全体をそこに移動して、具体的なクラスのメソッドのAbstractMyFieldset
違いを処理したいと考えています。init()
AbstractMyFooFieldset
namespace My\Form\Fieldset;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
abstract class AbstractMyFieldset extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add(
[
'type' => 'multi_checkbox',
'name' => 'my_field',
'options' => [
'label_attributes' => [
'class' => '...'
],
'value_options' => $this->getValueOptions()
]
]);
}
public function getInputFilterSpecification()
{
return [...];
}
protected function getValueOptions()
{
...
return $valueOptions;
}
}
MyFooServerFieldset
namespace My\Form\Fieldset;
use Zend\Form\Fieldset;
class MyFooServerFieldset extends AbstractMyFieldset
{
public function init()
{
parent::init();
$this->get('my_field')->setType('radio'); // There is not method Element#setType(...)! How to do this?
$this->get('my_field')->setAttribute('required', 'required'); // But this works.
}
}
および属性などtype
、要素の およびその他の構成を設定したいと考えています。属性の設定は問題ないようです。少なくとも属性を設定できます。しかし、タイプを設定できません -- がありません。type
required
required
Element#setType(...)
編集された後、type
のを設定する方法は?Zend\Form\Element
add