How to set custom attribute labels for nested inpu

2019-06-20 03:30发布

I'm trying to get custom labels in the error messages. My inputs are nested and i am able to validate them using dot notation (e.g. POLICY.HOLDER.NAME ). They have to be nested and all caps to match the db structure so I can easily mass assign them.

Using the same notation in 'attributes' => ['POLICY.HOLDER.NAME' => 'Policy Holder'] in resources/lang/en/validation.php yielded no result.

I'm just trying to get them to match the labels i've set in the form.

app/Http/Controllers/OfferController.php (just the interesting part)

public function postOffer(OfferRequest $request) { //
    $post_data = $request->all();
    if (isset($post_data['POLICY'])) {
        // code to get data from $_POST and assign to models
    }
}

app/Http/Requests/OfferRequest.php

<?php 
namespace App\Http\Requests;

use App\Http\Requests\Request, Auth;

class OfertaRequest extends Request
{

    public function authorize() {
        return Auth::check();
    }

    public function rules()
    {
        return [
            'POLICY.HOLDER.NAME' => 'required',
        ];
    }

    public function forbiddenResponse()
    {
        return Response::make('Permission denied foo!', 403);
    }

}

resources/lang/en/validation.php

'attributes' => [
    'POLICY.HOLDER.NAME' => 'Some custom string here...',
],

As you can see i've tried adding the input name in the Custom Validation Attributes array with no success. Here's the error message i get when leaving the input blank:

The p o l i c y. h o l d e r. n a m e field is required.

Note the spaces. I've tried that too, it didn't work.

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-20 03:44

Explicitly declare it as nested arrays:

'attributes' => [
    'POLICY' => [
        'HOLDER' => [
            'NAME' => 'Some custom string here...',
        ]
    ]
],
查看更多
登录 后发表回答