如何在Laravel中为嵌套输入设置自定义属性标签 [英] How to set custom attribute labels for nested inputs in Laravel

查看:410
本文介绍了如何在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...',
        ]
    ]
],

这篇关于如何在Laravel中为嵌套输入设置自定义属性标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆