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

[Validator] Default Validator group not validated #16070

Closed
pinoniq opened this Issue Oct 2, 2015 · 3 comments

Comments

Projects
None yet
6 participants
@pinoniq

pinoniq commented Oct 2, 2015

Symfony framework edition 2.7

I have the following Doctrine entity:

<?php

namespace AppBundle\Doctrine\Entity;

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

/**
 * Class Article
 * @package AppBundle\Doctrine\Entity
 *
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 * @Assert\GroupSequenceProvider
 */
class Article implements GroupSequenceProviderInterface
{
    /**
     * @var string
     *
     * @ORM\Column(type="text")
     * @Assert\NotBlank
     */
    private $content;

    /**
     * @var string
     *
     * @Assert\NotBlank(groups={"Promoted"})
     * @Assert\Length(max="128", groups={"Promoted"})
     *
     * @ORM\Column(name="promotional_title", type="string", length=128, nullable=true)
     */
    private $promotionalTitle;

    /**
     * Adds the promoted group
     * {@inheritdoc}
     */
    public function getGroupSequence()
    {
        $groups = array();
        if ($this->isPromoted()) {
            $groups[] = 'Promoted';
        }
        return $groups;
    }

}

This leads to SQL errors when $content is empty. The $content property is never validated.

Same happens if I set the following:

@Assert\NotBlank(groups={'Default'})
...
$groups=array('Default');

As in the documentation, the Validator should always validate the Asserts on the $content variable. this is however not the case here.

@xabbuh xabbuh added the Validator label Oct 2, 2015

@xelaris

This comment has been minimized.

Show comment
Hide comment
@xelaris

xelaris Oct 2, 2015

Contributor

Thank you for reporting this issue @pinoniq. I think the validator works as intended. The validation group related documentation states:

As you have already seen in the previous section, the Default group and the group containing the class name (e.g. User) were identical. However, when using Group Sequences, they are no longer identical. The Default group will now reference the group sequence, instead of all constraints that do not belong to any group.

This means that you have to use the {ClassName} (e.g. User) group when specifying a group sequence.

I guess, if you initialize the $groups array with the group named like your class Article, as in the docs code example, the validation will work as exptected.

As in the documentation, the Validator should always validate the Asserts on the $content variable.

Maybe there is an issue in the documentation. Where is this declared?

Status: Works for me

Contributor

xelaris commented Oct 2, 2015

Thank you for reporting this issue @pinoniq. I think the validator works as intended. The validation group related documentation states:

As you have already seen in the previous section, the Default group and the group containing the class name (e.g. User) were identical. However, when using Group Sequences, they are no longer identical. The Default group will now reference the group sequence, instead of all constraints that do not belong to any group.

This means that you have to use the {ClassName} (e.g. User) group when specifying a group sequence.

I guess, if you initialize the $groups array with the group named like your class Article, as in the docs code example, the validation will work as exptected.

As in the documentation, the Validator should always validate the Asserts on the $content variable.

Maybe there is an issue in the documentation. Where is this declared?

Status: Works for me

@pinoniq

This comment has been minimized.

Show comment
Hide comment
@pinoniq

pinoniq Oct 5, 2015

I had to re-read the docs a couple of times. I think it isn't clear at all about this. I added the {ClassName} so my groupSequence always returns an array with this group and all works.

I think the docs should be clearer about this that when using groupSequences, and not returning the {ClassName} simply doesn't validate the Asserts without any group assigned to them.

If I have time Ill try some rewording myself.

pinoniq commented Oct 5, 2015

I had to re-read the docs a couple of times. I think it isn't clear at all about this. I added the {ClassName} so my groupSequence always returns an array with this group and all works.

I think the docs should be clearer about this that when using groupSequences, and not returning the {ClassName} simply doesn't validate the Asserts without any group assigned to them.

If I have time Ill try some rewording myself.

@javiereguiluz

This comment has been minimized.

Show comment
Hide comment
@javiereguiluz

javiereguiluz Feb 8, 2016

Member

Closing it because at the end this wasn't a Symfony bug. I've created an issue in the docs repo to see if we can improve this: symfony/symfony-docs#6244

Member

javiereguiluz commented Feb 8, 2016

Closing it because at the end this wasn't a Symfony bug. I've created an issue in the docs repo to see if we can improve this: symfony/symfony-docs#6244

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