Symfony 2 中的可选嵌入表单 [英] Optional embed form in Symfony 2

查看:21
本文介绍了Symfony 2 中的可选嵌入表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统中有两个实体:PersonPhone,代码如下.

I have two entities in my system: Person and Phone as the following code.

class Person
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $lastName;

    /**
     * @ORM\OneToOne(targetEntity="Phone", cascade={"persist"})
     */
    private $phone;
};

class Phone
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="PhoneType")
     */
    private $type;

    /**
     * @ORM\Column(type="integer")
     */
    private $countryCode;

    /**
     * @ORM\Column(type="integer")
     */
    private $regionCode;

    /**
     * @ORM\Column(type="integer")
     */
    private $number;
};

我还有一个表单来创建和更新一个人(使用电话),所以我有一个 PersonType 有一个代表电话的嵌入表单(PhoneType).

I also have a form to create and update a person (with the phone), so I have a PersonType that have a embed form representing a phone (PhoneType).

我的问题是一个人可以选择拥有一部电话,但如果这个人有一部电话,则所有电话字段都是必需的.所以,如果用户在所有电话字段上什么都不写,这代表一个没有电话的人,这种情况是有效的.但如果用户至少填写了一个表单字段,则所有其他字段都是必需的.

My problem is that a person can has optionally a phone, but if the person has a phone, all the phone fields are required. So, if user write nothing on all phone fields, this represent a person without phone and this case is valid. But if the user fills at least one form field, all other fields are required.

如果没有填写所有电话字段,我尝试采取一种方法,将电话设置为 null,这是在 Person 实体中的 setPhone 上实现的.但是有一个空电话,Symfony 告诉我所有电话字段都是必需的,但没有填写.我相信 Symfony 不会验证手机,因为我认为 Symfony 会直接在个人实体上应用验证.有一个空电话,为什么告诉我所有电话字段都没有填写?

I try to take an approach by setting the phone on null if all phone fields are not filled, this was implemented on setPhone in Person entity. But having a null phone, Symfony tell me that all phone fields are required but are not filled. I believe that Symfony will not validate the phone because I suppose that Symfony will apply the validation directly on person entity. Having a null phone, why tell me that all phone fields are not filled?

有没有办法做我想做的事(最好不修改我所有的控制器和表单类型,即在实体或验证组件级别)?

Is there a way to do what I want (preferably without modify all my controllers and form types, that is, at entity or validation component level)?

抱歉,有一些没有提到,如果用户填写电话字段,则需要使用不同的验证器单独验证所有电话字段(检查字段是否是格式正确的数字,检查正确的长度等).但如果用户将所有电话字段留空,则应忽略每个字段的验证.

Sorry, there is something not mentioned, if user fill a phone field, all phone fields need to be validated separately with different validators (to check if a field is a well formed number, to check correct length, etc). But if user leaves empty all phone fields, the per-field validation should be ignored.

谢谢!

推荐答案

我想试试这个方法:

在您的电话实体中创建其他方法,该方法验证所有字段是否为空或所有字段都不为空(这两种情况都是正确的)-然后返回 true.如果某些字段为空而某些字段不返回 false.向您的新方法添加断言注释 - 这将是您的新约束.

create additional method in your Phone entity which validates if all fields are null or all fields are not null (these two cases are correct) - and then return true. If some fields are null and some aren't return false. Add an assert annotation to your new method - this will be your new constraint.

/**
 * @Assert\True(message = "Fill all fields or leave all them blank")
 */

这应该有效.

欲了解更多信息,请看这里:http://symfony.com/doc/master/book/validation.html#getters

For more information look here: http://symfony.com/doc/master/book/validation.html#getters

试试这个:

将您的自定义验证方法(此方法检查是否填充了任何电话字段)定义为回调(在类的顶部):

define your custom validation method (this one which check if any of phone fields is filled) as Callback (at the top of the class):

 * @Assert\Callback(methods={"checkPhoneFields"})
 */
 class Phone {

接下来,您标记必须使用验证组验证的字段,例如.

Next you mark field wich have to be validated with validation group, eg.

/**
 * @ORM\Column(type="string", length=16, groups={"phone_validation"})
 */
private $number;

最后一件事,在您的自定义约束方法中,如果任何字段不为空,您需要打开phone_validation"组:

And the last thing, in your custom constraint method you need to switch on "phone_validation" group if any of field isn't empty:

public function checkPhoneFields(ExecutionContext $context) {
    if (/* fields are not empty */) {
        $context->getGraphWalker()->walkReference($this, 'phone_validation', $context->getPropertyPath(), true);
    }

这应该可行.

这篇关于Symfony 2 中的可选嵌入表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆