LoginSignup
2
0

More than 5 years have passed since last update.

EC-CUBE3 Formのcollectionを使ったPOST値について

Last updated at Posted at 2017-01-25

現在あるプロジェクトでEC-CUBE3のプラグインを作っていますが、そこでFormに関して思いっきりハマってしまったところがあったので備忘録として書いておきたいと思います。

はじめは以下のように実装していました。

ConfigType.php
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
                'plg_api_value',
                'text',
                array(
                    'label' => false,
                    'mapped' => false,
                )
            );
        $builder->add(
                'plg_api_display_flg',
                'choice',
                array(
                    'choices'  => array('1' => '表示', '0' => '非表示'),
                    'multiple' => false,
                    'expanded' => true,
                    'mapped' => false,
                )
            );
    }
    public function getName()
    {
        return 'test';
    }
Event.php
    public function onAdminProductEditInitialize(EventArgs $event) {
        $data_set = [];
        // デフォルト値のセット
        foreach ($Test as $key => $t) {
            $data_set[$key]['plg_test_value'] = $t->getTestValue();
            $data_set[$key]['plg_test_name'] = $t->getTestName();
        }
        $builder = $event->getArgument('builder');
        // collectionフォームの作成
        $builder->add(
            'collection_test',
            'collection',
            array(
                'type' => 'test',
                'data' => $data_set,
                'allow_add' => true,
                'allow_delete' => true,
                'mapped' => false,
            )
        );
    }

デフォルト値を生成する部分はQiita用に情報を隠すためにTestクラスっていうもので書いています。

んで、この状態だとフォームはちゃんと表示されるけど、POSTされたあとに

Event.php
    public function onAdminProductEditComplete(EventArgs $event) {
         $data = $form->get('collection_product_other_info')->getData();
         //ここで$dataをみてもPOSTされてるはずの値が取得できない!
    }

getData()のところで値が取得できないので、色々と試していて半日ほど時間を費やしていました。

半日費やした結果、Typeの設定をしている部分でmappedを指定していたのがNGだったようで、以下のように修正。

ConfigType.php
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
                'plg_api_value',
                'text',
                array(
                    'label' => false,
                    // これがダメだった
                    //'mapped' => false,
                )
            );
        $builder->add(
                'plg_api_display_flg',
                'choice',
                array(
                    'choices'  => array('1' => '表示', '0' => '非表示'),
                    'multiple' => false,
                    'expanded' => true,
                    // これもダメ!
                    //'mapped' => false,
                )
            );
    }
    public function getName()
    {
        return 'test';
    }

collectionの場合はmappedの個別指定はしてはダメなようでした。
というかmappedって公式ドキュメント読んでもイマイチ何のためにあるのかわかりませんでした。

mapped
データ型: boolean
オブジェクトを読み込んでいるまたは書き込んでいる間、フィールドが無視されるようにしたい場合は、 mapped オプションを false にしてください。

symfony2の学習コストが高いと思う今日この頃。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0