Latest

Thursday, July 13, 2017

Converting Laravel validation message dot syntax into array

Asked by: Evgeniy


I'am using Laravel on server side. Let's imagine our controller receive two fields url [string] and data [array with index head]. We can validate data and customize errors messages with

$this->validate($request, [

    'url' => 'required',
    'data.head' => 'required',

], [

    'url.required' => 'The :attribute field is required',
    'data.head.required' => 'The :attribute field is required',
]);

If validation fails, Laravel send back response with json data

{
    "url": ["The url field is required"],
    "data.head": ["The data.head field is required"]
}

How we can convert response data to send json, as below?

{
    "url": ["The url field is required"],
    "data": {
        "head": ["The data.head field is required"]
    }
}


Source

No comments:

Post a Comment

Adbox