找到你要的答案

Q:How to set custom attribute labels for nested inputs in Laravel

Q:如何为嵌套的输入Laravel设置自定义属性标签

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.

我试图在错误信息中得到自定义标签。我输入的嵌套和我能够验证他们使用点符号(如policy.holder.name)。它们必须嵌套和所有大写匹配DB结构,这样我就可以容易地对它们进行质量分配。

使用相同的符号'属性' = & gt;[政策。持有人。名字= & gt;“投保人”]在资源/郎/ EN / validation.php没有产生结果。

我只是想让他们和我在表格中的标签相匹配。

应用程序/ HTTP /控制器/ offercontroller.php(只是有趣的部分)

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

应用程序/ HTTP请求/ 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);
    }

}

资源/郎/ EN / validation.php

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

正如您所看到的,我尝试在自定义验证属性数组中添加输入名称,但没有成功。这里是错误消息时,我得到的输入空白:

这是一个必需的领域。

注意空格。我也试过了,没用。

answer1: 回答1:

Explicitly declare it as nested arrays:

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

显式声明为嵌套数组:

'attributes' => [
    'POLICY' => [
        'HOLDER' => [
            'NAME' => 'Some custom string here...',
        ]
    ]
],
validation  laravel  laravel-5.1