Skip to content

Validator Documentation for Collection constraint Required/Optional options #2432

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

Merged

Conversation

benglass
Copy link
Contributor

Q A
Doc fix? yes
New docs? yes
Applies to 2.1
Fixed tickets 999

#999

for fields within a Collection.

Required and Optional Field Constraints
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Member

Choose a reason for hiding this comment

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

the heading line should be as long as the heading title

@benglass
Copy link
Contributor Author

Note: YML and XML examples for configuration are not included. Worked with weaverryan to try to come up with a working version in YML without success.

This gist contains the YML configuration I tried:
https://gist.github.com/afishnamedsquish/5277987

The issue was that the constraint for the field needed to be parsed into an instance of Required or Optional and instead it was always parsed into an array with a single instance of Required or Optional in it. Might be a YML limitation or just something wrong with the syntax I tried.


Even without ``allowMissingFields`` set to true, you can now omit the ``alternate_email`` property complete from the profileData array, since
it is ``Optional``. However, if the the ``personal_email`` field does not exist in the array there will be a
constraint violation that the field is missing, since it is ``Required``.
Copy link
Member

Choose a reason for hiding this comment

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

some line breaking missing.

@benglass
Copy link
Contributor Author

Fixed whitespace issues from your comments.

* @Assert\Collection(
* fields={
* "personal_email" = @Required({@Assert\NotBlank, @Assert\Email}),
* "alternate_email" = @Optional({@Assert\Email}),
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to have the 2 = lined up with eachother.

benglass added 2 commits April 1, 2013 12:26
Formatting changes per comments from @wouterj
Change 'example' to 'instance'.
}

Even without ``allowMissingFields`` set to true, you can now omit the ``alternate_email`` property
completely from the profileData array, since it is ``Optional``. However, if the the ``personal_email``
Copy link
Member

Choose a reason for hiding this comment

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

completely from the ``profileData`` array [...]

benglass added 2 commits April 1, 2013 12:53
Add variable indicators around profileData
Removed use statements for Required/Optional. Instead use Assert\Collection\Required.
@stof
Copy link
Member

stof commented Apr 4, 2013

you should add the YAML and XML mapping too

@wouterj
Copy link
Member

wouterj commented Apr 4, 2013

@stof it seems like this is not possible with Yaml and Xml. Afishnamedsquish, Ryan and I have tried it but can't come with a solution. Even bernhard didn't know the format, see also: https://gist.github.com/afishnamedsquish/5277987
If you know it, you're welcome to post it! ;-)

@stof
Copy link
Member

stof commented Apr 5, 2013

@wouterj This should be reported as a bug if YAML and XML don't support it. Can you open a ticket ?

@wouterj
Copy link
Member

wouterj commented Apr 6, 2013

@stof, yes I will do

weaverryan added a commit that referenced this pull request Apr 9, 2013
…optional_required_docs

Validator Documentation for Collection constraint Required/Optional options
@weaverryan weaverryan merged commit 2928045 into symfony:2.1 Apr 9, 2013
@weaverryan
Copy link
Member

Hey!

This is a really nice entry - very clearly explained, nice work! I did tweak some minor language at sha: cf049ed, because iirc, Required doesn't really make the key required, it just means that the validation rules will be applied always (and so the NotBlank really makes it required).

Thanks!

@xabbuh
Copy link
Member

xabbuh commented May 1, 2013

This works for me:

Acme\AuthorBundle\Entity\Author:
    properties:
        profileData:
             - Collection:
                fields:
                    alternate_email:
                        constraints:
                            - Collection\Optional:
                                - Email: ~

Can someone confirm?

@wouterj
Copy link
Member

wouterj commented May 1, 2013

negative for sf2.1 (didn't test it in other versions)

fabpot added a commit to symfony/symfony that referenced this pull request Jul 21, 2013
This PR was submitted for the 2.1 branch but it was merged into the 2.2 branch instead (closes #7930).

Discussion
----------

handle Optional and Required constraints from XML or YAML sources correc...

...tly

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7575
| License       | MIT
| Doc PR        | symfony/symfony-docs#2432

Commits
-------

a93a23a handle Optional and Required constraints from XML or YAML sources correctly
@xabbuh
Copy link
Member

xabbuh commented Jul 21, 2013

Now that symfony/symfony#7930 is merged. Is any work left here?

@benglass
Copy link
Contributor Author

An example of YML configuration could be added to the docs

@wouterj
Copy link
Member

wouterj commented Jul 22, 2013

And a xml example

@xabbuh
Copy link
Member

xabbuh commented Jul 22, 2013

You're right, see #2841 for this.

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

Successfully merging this pull request may close these issues.

5 participants