Today I found one issue with the CollectionType, related to Object
Validatoin. Let's imagine two objects:

class Website {
....
     /**
     * @orm:OneToMany(targetEntity="Domain", mappedBy="merchant",
cascade={"all"}, orphanRemoval=true)
     */
    private $domains;
....
}

and

class Domain {
....
    /**
     * @orm:Column(type="string")
     */
    private $url;


    /**
     * @orm:ManyToOne(targetEntity="Website", inversedBy="domains")
     */
    private $merchant;
....
}

and a DomainType:

class DomainType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options) {
        $builder->add('url','text',array(
            'required'          => false,
        ));
    }

    public function getDefaultOptions(array $options) {
        return array(
            'data_class' => 'My\Bundle\Entity\Domain',
        );
    }
}

We have Websites, and each website has one or more domains. I created
a Form using the CollectionType, to embed one more more DomainType
form, and with a little help of some small javascript, I can add and
remove Domains from my object. For this to work, I use the Collection
"prototype" in order to be able to dynamically create new entries in
my form (cloning the prototype and changing $$name$$ to my sequential
number - just like in 
https://github.com/beberlei/AcmePizzaBundle/blob/master/Resources/views/Order/index.html.twig).
So far so good, everything works fine.

The next step is to add validation to my objects, so a Domain's URL
cannot be left blank, and here's where the problem happens. Because of
the Collection prototype, the form contains an extra field, $$name$$,
that for obvious reason, has all of it's fields blank. CollectionType
sets the property_path of the prototype field to false, so this is not
sent to my object, BUT, it goes through the validation process
anyways, and it fails the NotBlank constraint. Looks like, when this
form is bound, it creates a Domain object for all of my form entries,
including the prototype one, and stuff them with data and tries to
validate it. As my prototype ($$name$$) has all fields blank, it
fails.

I think the right behavior here is to completely ignore the prototype
Type, or at least have a way to set default values to it, so it can
satisfy all constraints.

Any thoughts on this?

Thanks,
Carlos


-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to