我正在使用symfony中的嵌入表单,无法从嵌入表单中获取数据。
这是我的专利表格
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use AppBundle\Form\SubscriberAddressType;
class SubscriberDetailsType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('firstname', TextType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => [
'placeholder' => 'First Name',
'class' => 'form-control'
]])
->add('lastname', TextType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => array(
'placeholder' => 'Last Name',
'class' => 'form-control'
)])
->add('emailaddress', EmailType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => [
'placeholder' => 'Email Address',
'pattern' => '.{2,}',//minlength
'class' => 'form-control'
]])
->add('subscriberaddress', CollectionType::class, ['entry_type' => SubscriberAddressType::class])
->add('submit', SubmitType::class, [
'label' => 'Sign Up',
'attr' => [
'class' => 'smoothScroll btn btn-danger sb-button'
]
])
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(['data_class' => 'AppBundle\Entity\SubscriberDetails']);
}
/**
* @return string
*/
public function getName() {
return 'subscriberdetails';
}
}
这是我的孩子表格
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class SubscriberAddressType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('address1', TextType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => [
'placeholder' => 'Address 1',
'class' => 'form-control'
]])
->add('city', TextType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => array(
'placeholder' => 'City',
'class' => 'form-control'
)])
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(['data_class' => 'AppBundle\Entity\SubscriberAddress']);
}
/**
* @return string
*/
public function getName() {
return 'subscriber';
}
}
这就是我在我的树枝模板中调用嵌入表单的方法
<div class="form-group ">
{% for field in form.subscriberaddress %}
{{ form_label(field.city) }}
{{ form_widget(field.city) }}
{% endfor %}
</div>
当我尝试使用以下内容从表单中获取数据时:
$firstname = $form['firstname'] ->getData();
$lastname =$form['lastname'] ->getData();
$email = $form['emailaddress'] ->getData();
$address = $form['subscriber address']['address1'] ->getData();
$city =$form['subscriber address']['city'] ->getData();
它适用于前3行以上,但当它到达$ address = $ form ['subscriber address'] ['address1'] - &gt; getData(); with是一个嵌入字段,symfony发现一个错误的子元素不存在。
我试过去官方的symfony食谱以及这些帖子 Post1,Post2但这些似乎都没有回答我的问题,即:如何从表单中的嵌入字段中获取数据?
答案 0 :(得分:5)
您的嵌入表单实际上嵌入了集合中(不确定这是否是您的意图)所以您需要将其视为集合/数组
您可能想将其更改为
->add('subscriberaddress', SubscriberAddressType::class)
然后$ form-&gt; getData()将起作用。
或者,您可以拨打$form->get('subscriberaddress')->getData()
如果您确实希望将其用作CollectionType
来保留SubscriberAddressType
的多个副本,请通过
foreach ($form->get('subscriberaddress') as $subForm) {
$subData[] = $subForm->getData();
}
另请考虑将名称subscriberaddress
更改为subscriberAddresses
以表明其收藏