からその場で形成することelemetsを追加し、私はこのようなコントローラの工場からフォームを取得:ZF2 ZF2でデータベース
$this->add([
'type' => 'Zend\Form\Element\MultiCheckBox',
'name' => 'partyRoleIds',
'options' => [
'label' => 'Отношение',
'value_options' => [
[
'value' => '1',
'label' => 'client',
],
[
'value' => '2',
'label' => 'prospect'],
[
'value' => '6',
'label' => 'contractor',
],
],
],
]);
I:
class SomeControllerFactory implements FactoryInterface
{
public function CreateService(SeviceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
// other things from service manager
$registrationForm = $realServiceLocator->get('FormElementManager')
->get('Path\To\My\Form\RegistrationForm');
}
return new SomeController(
// controller dependencies, including $registrationForm
);
}
をRegistrationForm
では、私がMultiCheckBox
持っています配列を返すdbクエリからvalue_options
を取り込みたいとします。 Populatingは問題ではありませんが、この配列をRegistrationForm
への依存として渡す方法はわかりません。$registrationForm = $realServiceLocator->get('FormElementManager')->get('Path\To\My\Form\RegistrationForm');
という呼び出しでは、依存関係を追加する場所がないためです。
どうすればいいですか?
PS:質問を書き直し、私の初期の簡潔さを許してください。フォームが$ registrationFormであれば、あなたが書く、あなたのコントローラで
public function setValueOptions($element, array $values_options)
{
$e = $this->get($element);
$e->setValueOptions($values_options);
return $this;
}
:フォームクラスで
問題は何ですか?あなたはどこにいるのですか? – Rizier123
@ Rizier123、多分それはばかげていますが、私はどのようにフォームにこの配列を良い方法で渡すのか分かりません。私は工場コントローラでコントローラからフォームを呼び出しています。工場コントローラで、私は '$ realServiceLocator-> get( 'FormElementManager') -> get( 'Path \ To \ My \ Form \ RegistrationForm');'のようなフォームを取得するためにサービスマネージャを使用しています。この方法では、配列を 'RegistrationForm'に渡す方法がわかりません。 –
@ Rizier123、OPを更新しました。 –