Skip to content
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

Help with validation array via yaml mapping #7648

Closed
alexpts opened this issue Mar 17, 2017 · 2 comments
Closed

Help with validation array via yaml mapping #7648

alexpts opened this issue Mar 17, 2017 · 2 comments

Comments

@alexpts
Copy link
Contributor

alexpts commented Mar 17, 2017

I try validate assoc array via symfony validation.

I could validate array in php notation:

use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Validation;

$constraint = new Collection([
    'fields' => [
        'email' => new Email(),
        'person' => new Optional(new Email())
    ]
]);

$validator = Validation::createValidatorBuilder()->getValidator();

$errors = $validator->validate(['email' => 'email', 'name' => null], $constraint);

I want use yaml definition rule.

# validation.yml
Collection:
    name:
      - NotBlank
      - Length:
          min: 2
    email:
      - Email
$validator = Validation::createValidatorBuilder()
    ->addYamlMapping('validation.yml')
    ->getValidator();

$errors = $validator->validate(['email' => 'email', 'name' => null]);

Can someone help me check an assoc array with a description of the rules through yaml?

In the documentation, very little is described about working with arrays.

@HeahDude
Copy link
Contributor

HeahDude commented Mar 26, 2017

Hello @alexpts, the documentation of the Collection contraint looks pretty explicit to me in all format, I've only spotted a typo while checking it (see #7686).

To use yaml definition, you should use something like:

ClassName:
    properties:
        someArrayPropertyName:
            - Collection:
                fields:
                    name:
                        - NotBlank
                        - { Length: { min: 2 } }
                    email:
                        - Email

Your problem may come from the absence of model data to validate though, this is not needed in full PHP, but using yaml you cannot just define a constraint, you need to map a class and class properties to constraints. It means a simple PHP array cannot have mapped constraint through yml or xml.

Anyway I'm closing here, feel free to ask your question through one of our dedicated support channel.

@alexpts
Copy link
Contributor Author

alexpts commented Mar 27, 2017

Ok, Thanks!

xabbuh added a commit that referenced this issue Apr 8, 2017
… example (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[Validation Constraint References] Fixed Collection model example

Ref #7648.

Commits
-------

b1179c9 [Validation Constraint References] Fixed Collection model example
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

2 participants