3

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"]
    }
}

5 Answers 5

1

In javascript

Loop on errors

error: function (errors) {
    $.each(errors['responseJSON']['errors'], function (index, error) {
        var object = {};
        element = dotToArray(index);
        object[index] = error[0];
        validator.showErrors(object);
    });
}

convert in dot notation into array notation. i.e abc.1.xyz into abc[1][xyz]

function dotToArray(str) {
    var output = '';
    var chucks = str.split('.');
    if(chucks.length > 1){
        for(i = 0; i < chucks.length; i++){
            if(i == 0){
                output = chucks[i];
            }else{
                output += '['+chucks[i]+']';
            }
        }
    }else{
        output = chucks[0];
    }

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

Comments

1

On the PHP side, you can use the undot function of the Collection class:

collect($errors->toArray())->undot()

Comments

0

Laravel has an helper called array_set that transform a dot based notation to array.

I don't know how you send the errors via ajax, but you should be able to do something like that:

$errors = [];
foreach ($validator->errors()->all() as $key => $value) {
  array_set($errors, $key, $value);
}

Edit: But apparently, you should be able to not use the dot notation by Specifying Custom Messages In Language Files like this example:

'custom' => [
    'email' => [
        'required' => 'We need to know your e-mail address!',
    ],
],

4 Comments

I'am confusing. Why Laravel did not do converting dot syntax in response automatically?
I edited, my answer. I'm guessing, Laravel keeps the format that you specified. At least, that make sense to me, but don't see the point of using dot notation afterward.
Thank you for answear, of course I know about custom messages in separate files, but it is not decide my problem. I will use messages transformation before sending back.
Sorry, what I try to meant is, If you use the array notation, laravel should return the errors messages as array.
0

I don't know if it's still a valid question but to create a custom validation using dot notation in laravel, you can specify the array like this in your validation.php

'custom' => [
        'parent' => [
            'children' => [
                'required' => 'custom message here'
            ]
        ]

This will be the parent.children property.

see ya.

Comments

0
key = key.replace(/\./g, '[') + Array(key.split('.').length).join(']');

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

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.