简体   繁体   中英

silex validator using last constraint from previous collection item

based on the tutorial for validation in silex i've created a constraint:

    $constraint = new Assert\Collection([
        'token' => [
            new Assert\NotBlank(),
            new Assert\Length(['min' => Constant::TOKEN_LENGTH, 'max' => Constant::TOKEN_LENGTH]),
        ],
        'languageCode' => [
            new Assert\NotBlank(),
            new Assert\Choice(['choices' => [Constant::LANGUAGE_CODE_DE, Constant::LANGUAGE_CODE_EN]])
        ],
        'startDate' => [
            new Assert\NotBlank(),
            new Assert\Date()
        ],
        'endDate' => [
            new Assert\NotBlank(),
            new Assert\Date()
        ],
        'duration' => [
            new Assert\GreaterThanOrEqual(['value' => 1])
        ],
    ]);

which should match the following structures (here from json):

{
    "token": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
    "languageCode": "DE",
    "startDate": "2016-01-01",
    "endDate": "2016-01-05",
    "duration": 1
}

and

{
    "token": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
    "languageCode": "DE",
    "startDate": "2016-01-01",
    "endDate": "2016-01-05"
}

While first example is validated as expected, the second (with the optional duration) will respond in a strange behavior: the constraints from "endDate" where used:

object(Symfony\Component\Validator\ConstraintViolationList)[154]
  private 'violations' => 
    array (size=1)
      0 => 
        object(Symfony\Component\Validator\ConstraintViolation)[158]
          private 'message' => string 'This field is missing.' (length=22)
          private 'messageTemplate' => string 'This field is missing.' (length=22)
          private 'parameters' => 
            array (size=1)
              '{{ field }}' => string '"duration"' (length=10)
          private 'plural' => null
          private 'root' => 
            array (size=4)
              'token' => string 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' (length=64)
              'languageCode' => string 'DE' (length=2)
              'startDate' => string '2016-01-01' (length=10)
              'endDate' => string '2016-01-05' (length=10)
          private 'propertyPath' => string '[duration]' (length=10)
          private 'invalidValue' => null
          private 'constraint' => 
            object(Symfony\Component\Validator\Constraints\Date)[136]
              public 'message' => string 'This value is not a valid date.' (length=31)
              public 'payload' => null
              public 'groups' => 
                array (size=1)
                  0 => string 'Default' (length=7)
          private 'code' => string '2fa2158c-2a7f-484b-98aa-975522539ff8' (length=36)
          private 'cause' => null

I don't see the any reason for that....

try to add parameter allowMissingFields

$constraint = new Assert\Collection([
    'allowMissingFields' => true,
    'fields' => [asserts...]
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM