5

Is it possible to expand validator responses from nested arrays. Typically Laravel responds with the "dot notation" for example:

    [
        'organisation.name'          => 'required|max:60|min:3',
        'organisation.avatar'        => '',
        'organisation.phone'         => 'required|max:25|min:5',
        'organisation.paid_staff'    => 'required|numeric'
    ]

An error with organisation.name wold return:

{
  "message": "422 Unprocessable Entity",
  "errors": {
    "organisation.name": [
      "The organisation name has already been taken."
    ]
  },
  "statusCode": 422
};

Where i would like to have the dot notation expanded as such:

{
  "message": "422 Unprocessable Entity",
  "errors": {
    "organisation": {
        "name": [
            "The organisation name has already been taken."
        ]
    }
  },
  "statusCode": 422
};

Can anyone shed light on this?

1
  • I would also like to know if this is possible and how, great question Commented Feb 16, 2018 at 11:58

2 Answers 2

2

We achieved this outcome by extending the App\Exceptions\Handler class method invalidJson() as follows:

/**
 * Convert a validation exception into a JSON response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Validation\ValidationException  $exception
 * @return \Illuminate\Http\JsonResponse
 */
protected function invalidJson($request, ValidationException $exception)
{
    $jsonResponse = parent::invalidJson($request, $exception);

    $original = (array) $jsonResponse->getData();

    $jsonResponse->setData(array_merge($original, [
        'statusCode'    => $exception->status,
        'errors'        => Arrays::expandDotNotationKeys((array) $original['errors']),
    ]));

    return $jsonResponse;
}

We have a Utility class called Arrays with a method expandDotNotationKeys() that looks like this:

/**
 * Expands arrays with keys that have dot notation
 *
 * @param Array $array
 *
 * @return Array
 */
public static function expandDotNotationKeys(Array $array)
{
    $result = [];

    foreach ($array as $key => $value) {
      array_set($result, $key, $value);
    }

    return $result;
}

Done! no need to change native language files.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry but, i dont follow, what i have to do with this code?
0

I haven't done this myself yet, but looking at the docs give a hint -

In many cases, you may wish to specify your attribute specific custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.

Your Language File

'custom' => [
    'organisations' => [
        'name' => [
             'required' => 'The organisation name has already been taken!',
        ],
    ],
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.