Skip to content

[2.2] [Validator] "AnyOf" validation constraint #4861

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
wants to merge 9 commits into from

Conversation

olegstepura
Copy link
Contributor

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Todo: documentation (hopefully not me)
License of the code: MIT
Documentation PR: none

Here is proposal for AnyOf validation constraint. It's used to validate against any of given child validation constraints. If any of child validation constraints will pass validation of given value then AnyOf will pass as well.

<?php
// src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /**
     * @Assert\AnyOf({
     *     @Assert\Null
     *     @Assert\MinLength(3),
     * })
     */
     protected $secondName;
}

This will pass validation without violations if $secondName is a null OR if it's a string with a minimum length of 3.
Actually it was created to be used together with a more complex constraints.

@olegstepura
Copy link
Contributor Author

BTW I named it AnyOf instead of just Any so that noone think of it as an opposite to All validation constraint. It's purpose is totally different.

*/
class AnyOf extends Constraint
{
public $message = 'Value "{{ value }}" did not pass validation against any of constraints in the list [{{ constraints }}]';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong. we don't want to display a list of constraint classes to the visitor seing a form error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the code to only display all the child violations if validation against AnyOf failed. The only bad thing here is that there is no message that there had to be ANY of those to match. But merging those messages into AnyOf's own error message is not a good idea as well.

@olegstepura
Copy link
Contributor Author

@stof what do you think about constraint naming? I described the reason I named it so in the first comment but maybe it should be named just Any in spite of that?

@stof
Copy link
Member

stof commented Jul 12, 2012

I think AnyOf is better than Any to avoid confusion with All, as they are indeed totally different.

@olegstepura
Copy link
Contributor Author

Do you accept extending All? This prevents copy-pasting, but makes those classes connected where they maybe shouldn't be.

@Tobion
Copy link
Contributor

Tobion commented Jul 12, 2012

Your example with Null or MinLength is bad because MinLength alone also allows null. So in this case AnyOf is not even necessary.

@olegstepura
Copy link
Contributor Author

@Tobion Yes, I know, this is why I wrote that actually it was created to be used together with a more complex constraints.

The better example may be with Min(10) and Max(5) so that we do not accept 6-9 but do accept all other values.
If any further refactor will be needed I will also change Constraints used in tests.

@olegstepura
Copy link
Contributor Author

BTW, why does Symfony\Component\Validator\ConstraintViolationList still implement \IteratorAggregate, \Countable, \ArrayAccess by itself and not uses Doctrine\Common\Collections\ArrayCollection or some similar indoor solution to prevent copy-pasting of similar functions and allow for more usable methods to exist at ConstraintViolationList?

In this case ->toArray() method could be very helpful, for example. Using iterator_to_array() must be slower.

@stof
Copy link
Member

stof commented Jul 13, 2012

@olegstepura Using the Doctrine ArrayCollection would introduce a hard dependency to doctrine common in the component.

@olegstepura
Copy link
Contributor Author

@stof Then creating some symfony/common to store such common things makes sense. I'm sure this class is not the only one in the framework that implements all this interfaces, and each class implements it again and agian by itself.

There is Form, FormView, Options, ConstraintViolationList to name a few..

@stof
Copy link
Member

stof commented Jul 13, 2012

Having a common base class for them does not make sense. They are different concepts. It could be a use case for traits, but Symfony cannot drop the support of PHP 5.3.

@olegstepura
Copy link
Contributor Author

Well, ok, you know better. Any other improvements on this pull request needed to be done?

@stof
Copy link
Member

stof commented Jul 13, 2012

Well, the description marks it for 2.2, so it is not the priority (priority is to release beta3)

@stof
Copy link
Member

stof commented Oct 13, 2012

@bschussek @fabpot ping

use Symfony\Component\Validator\ConstraintValidator;

/**
* AnyValidator class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be equals name of the class or just removed

pborreli and others added 7 commits October 28, 2012 23:25
This PR was merged into the master branch.

Commits
-------

a7ce6be Fixed typos

Discussion
----------

Fixed typos
This PR was merged into the master branch.

Commits
-------

2817a47 [Finder] Fixed filename containing space bug in gnu adapter.
9bf7cb0 [Finder] Added filename containing space to tests.

Discussion
----------

[Finder] Fixed filename containing space bug in gnu find adapter.

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes: symfony#5851

`GNU find` adapter now uses `cut` instead of `awk`.
…o any_of_validator

Conflicts:
	src/Symfony/Component/Validator/Constraints/AnyOfValidator.php
@webmozart
Copy link
Contributor

See also #5749.

Do we really need this? In #5749, which was closed today, no real need was seen for this functionality.

@olegstepura
Copy link
Contributor Author

@bschussek I'm not sure why you created those functionality, but I did that when I had a need for it. It was not an abstract function. But since it's open already for 5 months and no one can tell if this will ever be merged maybe it'll be closed as well sometimes.

@webmozart
Copy link
Contributor

@olegstepura Yes, let's close this until more people cry for it - then at least we'll be able to have a lively discussion about it. :)

@webmozart webmozart closed this Dec 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants