2017-06-28 4 views
1

を使用して共有一人として、私は私が共有インスタンスとして使用したい形式を持つフォームを定義することはできませんよ。しかし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 sharedZF2 - 私は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', 
       ] 
      ] 
     ); 
    } 
} 

定義SampleFormModule.php

しかし、getFormElementConfigをに定義すると、期待通りに機能します。としては、以下:

私は実際のフォームクラス名への参照を提供 sharedキーで
public function getFormElementConfig() 
{ 
    return [ 
     'invokables' => [ 
      'MyProject\Form\SharedSampleForm' => SampleForm::class, 
     ], 
     'aliases' => [ 
      'sharedSampleForm' => 'MyProject\Form\SharedSampleForm' 
     ], 
     'shared' => [ 
      SampleForm::class => true 
     ] 
    ]; 
} 

すなわち。

同じ定義がgetServiceConfig()の下で定義されている場合、期待どおり、それはそのようなエラーをスローせずに動作します。

sharedのサービス名を使用して実際のクラス参照を提供するにはどうすればよいか教えてください。

答えて

-1

getFormElementConfig()は、フォーム要素の定義に使用されます。 をサービスとして定義するために使用されません。このフォームをServiceと定義する場合は、getServiceConfig()の下に定義する必要があります。

もう一つのヒント、あなたがエイリアスを作成している場合、それのクラス名を使用してService名前を定義します。

public function getServiceConfig() 
{ 
    return [ 
     'invokables' => [ 
      SampleForm::class => SampleForm::class, 
     ], 
     'aliases' => [ 
      'sharedSampleForm' => SampleForm::class 
     ], 
     'shared' => [ 
      SampleForm::class => true 
     ] 
    ]; 
} 

フォーム定義は常に行く必要があるため、あなたは `)、私の知る限りではまあこの$this->getServiceLocator()->get('sharedSampleForm');

+1

のように' getServiceConfig(でフォームを定義し、その正しくない大会をエイリアス名を使用してフォームを呼び出すことができます'getFormElementConfig()'の下にあります。ご回答ありがとうございますが、私はそれを正しい解決策とは考えていません。 2番目の提案に関しては、フォームの定義時にフォームの特定の名前が必要なので、クラス名と同じ名前を定義することはできません。 –

+0

これをチェックしてください。カスタムフォーム要素を定義するために 'getFormElementConfig()'が使われます。フォームは定義されていません。https://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of -forms.html#creating-custom-elements –

+1

'getFormElementConfig()'はカスタム要素を定義することに同意します。しかし、Formクラス( 'Zend \ Form \ Form')に気付くと、それ自体が' Elementset 'を 'Fieldset'を通して内部的に拡張しているので、フォームクラス自体はカスタム要素です。そして、それは 'getFormElementConfig()'の中で定義されるべきです。 –

関連する問題