0

How to get this full propertyPath? Need this to add validation cases in jQuery, because I've rewrite legacy application into new SF4 application. Odx2ParameterCollectionType is a collection with dynamic fields.

Is there any way to get this validation errors? Maybe You've other solutions how to work with it?

protected function renderFormErrors(FormInterface $form): JsonResponse
{
    $errors = [];

    foreach ($form->getErrors(true) as $error) {
        $errors[$error->getOrigin()->getName()] = $error->getMessage();
    }

    return $this->json([
        self::RETURN_PARAMETER_STATUS => Response::HTTP_INTERNAL_SERVER_ERROR,
        self::RETURN_PARAMETER_ERRORS => $errors
    ]);
}

Actual:

{
  "status": 500,
  "errors": {
    "udsId": "This value should not be blank."
  }
}

Expected:

{
  "status": 500,
  "errors": {
    "Odx2ParameterCollectionType[parameters][5][udsId]": "This value should not be blank."
  }
}

EDIT:

Ok, I've figure it out, but for now I've one missing thing - index of collection, it's my code:

    foreach ($form->all() as $name => $child) {
        if (!$child->isValid()) {
            foreach ($child->getErrors(true) as $error) {
                $errors[(string) $form->getPropertyPath()][$name][$error->getOrigin()->getName()] = $error->getMessage();
            }
        }
    }
Largo
  • 159
  • 10

2 Answers2

3

Please take a look at this course. It can really help with these kinds of problems https://symfonycasts.com/screencast/symfony-rest2/api-problem-class

So, if you only want to get the actual path for your invalid field and message, here is one way to do that

<?php
namespace App\Form\Validation;

use Symfony\Component\Form\FormInterface;

class ValidateForm
{
    static function renderFormErrors(FormInterface $form)
    {
        $errorModel = new FormValidation();

        foreach ($form->getErrors(true) as $error)
        {
            $path = $error->getCause()->getPropertyPath();
            if (empty($path)) {
                break;
            }

            $path = preg_replace("/^(data.)|(.data)|(\\])|(\\[)|children/", '', $path);
            $message = $error->getCause()->getMessage();
            $errorModel->errors[$path] = $message;
        }

        return $errorModel; // or convert to json and return. 
    }
}

class FormValidation
{
    public $message ="Validation Error";
    public $errors = [];
}
Nairi Abgaryan
  • 616
  • 5
  • 16
2
    const REGEX_EXPLODE_COLLECTION_PATH = "/^(data.)|(.data)|(\\])|(\\[)|children/";

    $errors = [];

    foreach ($form->all() as $name => $child) {
        if (!$child->isValid()) {
            foreach ($child->getErrors(true) as $error) {
                $propertyPath = $error->getCause()->getPropertyPath();
                $path = preg_replace(self::REGEX_EXPLODE_COLLECTION_PATH, '', $propertyPath);

                list($collection, $index, $field) = explode('.', $path);

                $errors[(string) $form->getPropertyPath()][$collection][$index][$field] = $error->getMessage();
            }
        }
    }

Thanks Nairi Abgaryan, it's my solution that works for me perfectly :-)

Largo
  • 159
  • 10