Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Sep 23, 2022
1 parent acbc896 commit 45b892f
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1369,29 +1369,43 @@ protected function handleXmlHttpRequestErrorResponse(Request $request, FormInter

private function buildConstraintViolationList(FormInterface $form): ConstraintViolationList
{
$closure = \Closure::bind(function (ConstraintViolation $violation, string $newPropertyPath): ConstraintViolation {
/**
* @psalm-suppress InaccessibleProperty
*/
$violation->propertyPath = $newPropertyPath;

return $violation;
}, null, ConstraintViolation::class);

$errors = new ConstraintViolationList();

foreach ($form->getErrors(true) as $formError) {
$formName = $this->buildName($formError->getOrigin());
$closure = \Closure::bind(function (ConstraintViolation $violation) use ($formName): ConstraintViolation {
$violation->propertyPath = $formName;
$origin = $formError->getOrigin();
$cause = $formError->getCause();

return $violation;
}, null, ConstraintViolation::class);
if (null === $origin || !$cause instanceof ConstraintViolation) {
continue;
}

$errors->add($closure($formError->getCause()));
/**
* @psalm-suppress PossiblyInvalidFunctionCall
*/
$errors->add($closure($cause, $this->buildName($origin)));
}

return $errors;
}

private function buildName(FormInterface $form): string
{
if (!$form->getParent()) {
$parent = $form->getParent();

if (null === $parent) {
return $form->getName();
}

return $this->buildName($form->getParent()).'['.$form->getName().']';
return $this->buildName($parent).'['.$form->getName().']';
}

/**
Expand Down

0 comments on commit 45b892f

Please sign in to comment.