0

このような国フィールドタイプを追加する FormType を作成しました

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('address', 'text', array(
            'required' => false,
        ))
        ->add('country', 'country', array(
            'required' => false,
        ));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => $this->dataClass,
    ));
}

public function getName()
{
    return 'form_contact';
}

このフォームのdata_classオプションを、住所を含むこれら 2 つのフィールドが両方とも文字列型であるエンティティに設定します。

フォームを書き換えて送信しても問題ありません。POST パラメータには次のものが含まれます

form_contact['address'] = 'Some value'
form_contact['country'] = 'US'

しかし、送信されたフォームからエンティティを永続化すると、国フィールドが NULL になります。Dump $form->getData()私はこれを得ました:

object(Namspace\Entity\MyEntity)#3501 (3) {
  ["id":protected]=>
  NULL
  ["address1":protected]=>
  string(12) "Some value"
  ["country":protected]=>
  NULL
  ["US"]=>
  string(2) "US"
}

国は米国であり、キーと値が米国の新しい要素ではないと思います。手伝って頂けますか?どうもありがとう

4

1 に答える 1