У меня есть два очень похожих Fieldset
s MyFooFieldset
и MyBarFieldset
. Для того, чтобы избежать дублирования кода, я создал AbstractMyFieldset
, переместил весь код там, и хотите обрабатывать различия в init()
методы конкретных классов:Как установить тип элемента в дочернем классе Fieldset в Zend Framework 2?
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
ред?