On 28/10/2020 16:58, Nicolas Grekas wrote:
about why we'd need nested attributes, here is a discussion about nested validation constraints:
https://github.com/symfony/symfony/issues/38503


Thanks, it's useful to see some real-world examples. As I suspected, nearly all of these explicitly take a *list* of nested attributes, not a single attribute.

> While most of the constraints receive the nested constraints as simple array, |Collection| however requires a mapping (field to constraint) and is usually combined with other composite constraints, which gives us a second nesting level.


There is much discussion of Collection as an edge case, but although the nested attributes are indeed inside a mapping, the *value* of that mapping is again a list of attributes. Single items are allowed, but this seems to be a special case for convenience.

In the below example from https://symfony.com/doc/current/reference/constraints/Collection.html the @Assert\Email could equivalently be written as an array with one item:

    /**
     * @Assert\Collection(
     *     fields = {
     *         "personal_email" = @Assert\Email,
     *         "short_bio" = {
     *             @Assert\NotBlank,
     *             @Assert\Length(
     *                 max = 100,
     *                 maxMessage = "Your short bio is too long!"
     *             )
     *         }
     *     },
     *     allowMissingFields = true
     * )
     */


This reinforces my earlier suggestion (https://externals.io/message/111936#112109) that #[Foo] in a nested context can simply imply an array of one attribute, rendering the above as:

#[Assert\Collection(
    fields: [
         "personal_email" => #[Assert\Email],
         "short_bio" => #[
                Assert\NotBlank,
                Assert\Length(
                      max: 100,
                      maxMessage: "Your short bio is too long!"
               )
        ]
    ],
    allowMissingFields: true
]


Regards,

--
Rowan Tommins (né Collins)
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to