5

我已将用户的帐户区域拆分为不同的表单,然后对于注册表单,我将这些位组合在一起,如下所示:

class RegistrationFormType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', 'email', array(
            'label' => '* Email address:'
        ))
        ->add('account_personal', 'my_personalinfo_form', array(
            'property_path' => 'account'
        ))
        ->add('account_contact', 'my_contactinfo_form', array(
            'property_path' => 'account'
        ))
    ;
}

问题是错误消息account_personal冒泡到表单的顶部。例如,如果名字在个人信息表单中留空,则“请输入您的名字”。“个人”和“联系”表格在他们自己的页面上以自己的形式正常工作。

的错误消息account_contact很好,并出现在正确的字段旁边。

但是,如果我交换->add上面的两个位(所以account_contact首先),那么问题就会逆转;工作错误消息account_personal现在在其相应字段旁边显示得很好,但现在错误消息account_contact冒泡到顶部!

任何建议都非常感谢!

- - - 编辑 - - -

个人信息表:

class PersonalInfoType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text')
        ->add('first_name', 'text', array(
            'required' => true,
            'label' => '* First name:'
        ))
        ->add('last_name', 'text', array(
            'required' => true,
            'label' => '* Surname:'
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'My\UserBundle\Entity\Account',
        'validation_groups' => array('personalInfo')
    ));
}

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

联系信息表:

class ContactInfoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('postcode', 'text', array(
            'required' => true
        ))
        ->add('address_1', 'text', array(
            'required' => true
        ))
        ->add('address_2', 'text', array(
            'required' => false
        ))
        ->add('address_3', 'text', array(
            'required' => false
        ))
        ->add('town', 'text')
        ->add('phone_daytime', 'text', array(
            'required' => true
        ))
        ->add('phone_mobile', 'text', array(
            'required' => true
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'My\UserBundle\Entity\Account',
        'validation_groups' => array('contactInfo')
    ));
}

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

以及要完成的 Account 实体:

/**
 * My\UserBundle\Entity\Account
 *
 * @ORM\Entity
 * @ORM\Table(
 *      name="accounts"
 * )
 */
class Account
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=25, nullable=true)
 * @Assert\Choice(choices = {"Mr", "Mrs", "Miss", "Ms", "Dr", "Prof"}, message = "Choose a valid title", groups={"personalInfo"})
 */
protected $title;

/**
 * @ORM\Column(type="string", length=150, nullable=true)
 * @Assert\NotBlank(message="Please enter your first name", groups={"personalInfo"})
 * @Assert\Length(max=150, maxMessage="null|Your first name must have less than {{ limit }} characters", groups={"personalInfo"})
 * @Assert\Regex(
 *     pattern="/\d/",
 *     match=false,
 *     message="Your name cannot contain a number"
 * )
 */
protected $first_name;

/**
 * @ORM\Column(type="string", length=150, nullable=true)
 * @Assert\NotBlank(message="Please enter your last name", groups={"personalInfo"})
 * @Assert\Length(max=150, maxMessage="null|Your last name must have less than {{ limit }} characters", groups={"personalInfo"})
 * @Assert\Regex(
 *     pattern="/\d/",
 *     match=false,
 *     message="Your name cannot contain a number"
 * )
 */
protected $last_name;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @MyAssert\Phone(message="Your daytime phone number is not valid", groups={"contactInfo"})
 */
protected $phone_daytime;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @MyAssert\MobilePhone(message="Your mobile phone number is not valid", groups={"contactInfo"})
 */
protected $phone_mobile;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank(message="Please enter the first line of your address", groups={"contactInfo"}
 * @Assert\Length(max=250, maxMessage="null|The first line of your address must have less than {{ limit }} characters", groups={"contactInfo"})
 */
protected $address_1;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @Assert\Length(max=250, maxMessage="null|The second line of your address must have less than {{ limit }} characters", groups={"contactInfo"})
 */
protected $address_2;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @Assert\Length(max=250, maxMessage="null|The third line of your address must have less than {{ limit }} characters", groups={"contactInfo"})
 */
protected $address_3;

/**
 * @ORM\Column(type="string", length=45, nullable=true)
 * @Assert\NotBlank(message="Please enter your town", groups={"contactInfo"})
 * @Assert\Length(max=45, maxMessage="null|Your town name must have less than {{ limit }} characters", groups={"contactInfo"})
 */
protected $town;

/**
 * @ORM\Column(type="string", length=45, nullable=true)
 * @Assert\NotBlank(message="Please enter your postcode", groups={"contactInfo"})
 * @MyAssert\Postcode(message="Invalid postcode entered", groups={"contactInfo"})
 */
protected $postcode;

 ... etc
4

1 回答 1

4

问题在于以一种形式两次使用相同的属性路径。我在这里提出了一个问题:

https://github.com/symfony/symfony/issues/5578

我在上面发布了这个解决方案:

我想出了一个不同的方法,即使用虚拟字段创建一个新的 FormType 来包含 2 个嵌入的表单:

class RegistrationAccountFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('account_personal', 'my_personalinfo_form', array(
            'data_class' => 'My\UserBundle\Entity\Account',
            'virtual' => true
        ))
        ->add('account_contact', 'my_contactinfo_form', array(
            'data_class' => 'My\UserBundle\Entity\Account',
            'virtual' => true
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'My\UserBundle\Entity\Account'
    ));
}

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

然后将主 RegistrationFormType 中的这些字段替换为

->add('account', new RegistrationAccountFormType)

这似乎是这样做的“正确”方式,它使 property_paths 在表单中保持唯一,并且子表单中也不需要 property_path 更改。

于 2012-10-03T23:01:32.240 回答