mardi 28 juin 2016

Custom validation message for json request payload

I'm using Laravel 5.2 and I'm submitting a json payload for my store and update controller methods. E.g.:

{
    "comments": "asdf",
    "materialId": "57719e5a907751.22478980",
    "orderData": {
        "advertiser_name": "123",
        "color": null,
        "email": "",
        "issue_date": "016-06-22",
        "name": "",
        "order_number": "123",
        "phone": "",
    },
    "uploadData": [
        {
            "clickthrough": "",
            "name": ""
        },
        {
            "clickthrough": null,
            "name": "Screen Shot 2016-06-24 at 3.46.59 PM.png"
        }
    ]
}

I have a Form Request Validation class defined. I've defined the rules I want to use and the custom error messages I'd like to use:

class MaterialUploadRequest extends Request
{

    public function rules()
    {
        return [
            'orderData.name' => 'required',
            'orderData.email' => 'required|email',
            'orderData.advertiser_name' => 'required',
            'orderData.issue_date' => 'required|date',
            'uploadData.*.name' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'orderData.name.require' => 'You must provide a name.',
            'orderData.email.require' => 'You must provide an email.',
            'orderData.email.email' => 'The email you provide must be a valid email.',
            'orderData.advertiser_name.require' => 'You must specify an advertiser name.',
            'orderData.issue_date.require' => 'You must specify an issue date.',
            'orderData.issue_date.date' => 'The issue date you specify must be a valid date.',
            'uploadData.*.name' => 'You must upload at least one file.',
        ];
    }
}

The validation rules work fine, but the custom messages are never applied. Using the payload below, the error I get for the orderData.name value being missing is still:

"The uploadData.0.name field is required."

Instead of the custom message:

"You must provide a name."

Seeing the 0 in the error message returned makes me think that the logic to look for the custom message is assuming that orderData.name is an array in dot notation instead of a javascript object. That said, I'm not sure exactly how to get this to work.

Any ideas?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2975erI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire