Symfony2空表单删除实体

时间:2015-03-04 20:25:12

标签: forms validation symfony

我有一个User实体,其中包含$ name,$ gender,$ email,$ password等等。这还包括一个$ postalAddress,它与具有$ city,$ postalCode,$ streetName和$ streetNumber的PostalAddress实体具有ManyToOne关系。

我有一个注册表单,您可以在其中填写整个postalAddress字段或不填写。到目前为止,这是有效的,但问题出现了:

还有另一种表单,当您已有帐户时,您可以在其中更改用户详细信息。如果已经给出了一个地址,并且您清空了所有postalAddress字段,则验证器不允许此操作,即使使用的UserType / PostalAddressType和验证完全相同。我的目标是您可以将此字段留空以删除地址(在User实体中将$ postalAddress设置为NULL并删除postalAddress整数,如果没有其他用户引用它)

有没有办法让用户删除现有地址?

这是validation.yml:

\Entity\User:
    properties:
        postalAddress:
        - Valid: ~
\Entity\PostalAddress:
properties:
    postalCode:
        - NotBlank:
            groups: [flow_createRegistration_step1, default]
        - Type:
            groups: [flow_createRegistration_step1, default]
            type: numeric
            message: "-"
        - Length:
            groups: [flow_createRegistration_step1, default]
            min: 5
            max: 5
            exactMessage: "-"
    city:
        - NotBlank:
            groups: [flow_createRegistration_step1, default]
            message: "field_required"
    streetName:
        - NotBlank:
            groups: [flow_createRegistration_step1, default]
            message: "field_required"
    streetNumber:
        - NotBlank:
            groups: [flow_createRegistration_step1, default]
            message: "field_required"
        - Type:
            groups: [flow_createRegistration_step1, default]
            type: numeric
            message: "-"

UserType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    ...
    $builder->add(
        'postalAddress',
        new PostalAddressType(),
        array(
            'label' => false,
            'required' => false
        )
    );
    ...
}

PostalAddressType.php:

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('streetName', 'text', array(
        'label' => 'Straße',
        'required' => true
    ));
    $builder->add('streetNumber', 'text', array(
        'label' => 'Nummer',
        'required' => true,
        'max_length' => 5,
        'attr' => array('class' => 'input-mini')
    ));
    $builder->add('postalCode', 'text', array(
        'label' => 'PLZ',
        'required' => true,
        'max_length' => 5,
        'attr' => array('class' => 'input-mini')
    ));
    $builder->add('city', 'text', array(
        'label' => 'Ort',
        'required' => true
    ));
    }

0 个答案:

没有答案