ApiExceptionBundle
ApiExceptionBundle copied to clipboard
Flattern exception on collection index seems not to work
Hi,
I borrowed your code from ViolationFormException::getFlatternErrors
for my project as I don't need extra stuff your bundle is doing.
I have an error at an index of a collection of fields but the method is not keeping the index. See attachment. The fields in red are in error but only the lines 2 and 3 should be red. My validation is column 2 value of current row should be greater than or equals to column 3 of previous row.
In the following example : on the row with 2%, 200 should be greater than or equals to 300 of the row with 1%. Same apply on next row : 250 should be greater than or equals to 300 of the row with 2%.
Is there a way to set the index properly ? Here is my validation callback, applied on the array:
new Callback([
// $steps catains my collection with ['percentage' => 1, 'min' => 100, 'max' => 300] as first item.
'callback' => function (array $steps, ExecutionContextInterface $context) {
if (($count = \count($steps)) <= 1) {
return;
}
$parentStep = $steps[0];
$index = 1;
while ($index < $count) {
if ($parentStep['max'] > $steps[$index]['min']) {
$context
->buildViolation('La fourchette minimale est inférieure à la fourchette maximale de la ligne précédente.')
->atPath($index)
->addViolation();
}
$parentStep = $steps[$index];
++$index;
}
},
])
But the flattened errors are always starting at index 0
but should instead keep the given index. How can it be fixed?
Thank you
Found an alternative to attach the violation atPath(sprintf('[%d][min]', $index))
which also can sound more logical as the error is on this field. But Still, would it be possible to attach the error at index level?
Thank you