Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Cascade validation groups from GroupSequence instead of "Default" #15666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
teohhanhui opened this issue Sep 1, 2015 · 2 comments

Comments

@teohhanhui
Copy link
Contributor

How do I achieve the following (similar to how Serializer groups work)? I'm not using Forms.

// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\GroupSequenceProviderInterface;

/**
 * @ORM\Entity
 */
class Product implements GroupSequenceProviderInterface
{
    /**
     * @ORM\Column(type="text", nullable=true)
     *
     * @Assert\Type(type="string")
     * @Assert\NotBlank(groups={"Product_Active"})
     */
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="Offer")
     *
     * @Assert\Valid(traverse=true)
     */
    protected $offers;

    public function getGroupSequence()
    {
        $groups = ['Product'];

        if ($this->isActive()) {
            $groups[] = 'Product_Active';
        }

        return $groups;
    }
}
// src/AppBundle/Entity/Offer.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Offer
{
    /**
     * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
     *
     * @Assert\Type(type="string")
     * @Assert\Range(min=0)
     * @Assert\NotNull(groups={"Product_Active"})
     */
    protected $price;
}
@teohhanhui teohhanhui changed the title [Validator] Cascade validation groups from GroupSequence instead of Default [Validator] Cascade validation groups from GroupSequence instead of "Default" Sep 1, 2015
@rpg600
Copy link
Contributor

rpg600 commented Sep 6, 2015

Check #15659, this pr should fix your use case if merged.

@webmozart
Copy link
Contributor

See #15659 (comment) for a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants