validation - silex validator using last constraint from previous collection item -
based on tutorial validation in silex i've created 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 following structures (here 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 validated expected, second (with optional duration) respond in strange behavior: constraints "enddate" used:
object(symfony\component\validator\constraintviolationlist)[154] private 'violations' => array (size=1) 0 => object(symfony\component\validator\constraintviolation)[158] private 'message' => string 'this field missing.' (length=22) private 'messagetemplate' => string 'this field 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 not 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 reason that....
try add parameter allowmissingfields
$constraint = new assert\collection([ 'allowmissingfields' => true, 'fields' => [asserts...] ]);
Comments
Post a Comment