Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Flattern exception on collection index seems not to work #17

Open
flohw opened this issue Oct 23, 2020 · 1 comment
Open

Flattern exception on collection index seems not to work #17

flohw opened this issue Oct 23, 2020 · 1 comment

Comments

@flohw
Copy link

flohw commented Oct 23, 2020

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%.
2020-10-23-155524_1171x197_scrot

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

@flohw
Copy link
Author

flohw commented Oct 26, 2020

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

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

No branches or pull requests

1 participant