如何在Laravel中为嵌套输入设置自定义属性标签

如何在Laravel中为嵌套输入设置自定义属性标签

问题描述:

我正在尝试在错误消息中获取自定义标签.我的输入是嵌套的,我可以使用点表示法(例如POLICY.HOLDER.NAME)对其进行验证.它们必须嵌套并且所有大写字母都必须与db结构匹配,这样我才能轻松地批量分配它们.

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.

resources/lang/en/validation.php中的'attributes' => ['POLICY.HOLDER.NAME' => 'Policy Holder']中使用相同的表示法不会产生任何结果.

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(只是有趣的部分)

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

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/zh-CN/validation.php

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.

将其明确声明为嵌套数组:

Explicitly declare it as nested arrays:

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