を使用して共有一人として、私は私が共有インスタンスとして使用したい形式を持つフォームを定義することはできませんよ。しかしshared
キーは、割り当てられた名前ではなく実際のクラスだけを受け入れます。私の問題をよりよく理解するためのコードスニペットの一部を共有: Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\Form\FormElementManager\FormElementManagerV2Polyfill::setShared: A service by the name "MyProject\Form\SharedSampleForm" was not found and could not be marked as shared
ZF2 - 私はZendFramework 2に取り組んでいます、その名前だけでなく、別名
namespace MyProject;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\FormElementProviderInterface;
use MyProject\Form\SampleForm;
class Module implements ConfigProviderInterface, FormElementProviderInterface
{
/**
* {@inheritDoc}
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
/**
* {@inheritdoc}
*/
public function getFormElementConfig()
{
return [
'invokables' => [
'MyProject\Form\SharedSampleForm' => SampleForm::class,
],
'aliases' => [
'sharedSampleForm' => 'MyProject\Form\SharedSampleForm'
],
'shared' => [
'MyProject\Form\SharedSampleForm' => true
]
];
}
}
:
SampleForm.php
namespace MyProject\Form;
use Zend\Form\Form;
class Sampleform extends Form
{
public function __construct()
{
parent::__construct('sampelname');
}
/**
* Initialize the form elements
*/
public function init()
{
$this->add(
[
'type' => 'Text',
'name' => 'name',
'options' => [
'label' => 'Enter your name',
]
]
);
}
}
定義SampleForm
Module.php
に
しかし、getFormElementConfig
をに定義すると、期待通りに機能します。としては、以下:
shared
キーで
public function getFormElementConfig()
{
return [
'invokables' => [
'MyProject\Form\SharedSampleForm' => SampleForm::class,
],
'aliases' => [
'sharedSampleForm' => 'MyProject\Form\SharedSampleForm'
],
'shared' => [
SampleForm::class => true
]
];
}
すなわち。
同じ定義がgetServiceConfig()
の下で定義されている場合、期待どおり、それはそのようなエラーをスローせずに動作します。
shared
のサービス名を使用して実際のクラス参照を提供するにはどうすればよいか教えてください。
のように' getServiceConfig(でフォームを定義し、その正しくない大会をエイリアス名を使用してフォームを呼び出すことができます'getFormElementConfig()'の下にあります。ご回答ありがとうございますが、私はそれを正しい解決策とは考えていません。 2番目の提案に関しては、フォームの定義時にフォームの特定の名前が必要なので、クラス名と同じ名前を定義することはできません。 –
これをチェックしてください。カスタムフォーム要素を定義するために 'getFormElementConfig()'が使われます。フォームは定義されていません。https://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of -forms.html#creating-custom-elements –
'getFormElementConfig()'はカスタム要素を定義することに同意します。しかし、Formクラス( 'Zend \ Form \ Form')に気付くと、それ自体が' Elementset 'を 'Fieldset'を通して内部的に拡張しているので、フォームクラス自体はカスタム要素です。そして、それは 'getFormElementConfig()'の中で定義されるべきです。 –