2

我正在使用 Laravel 构建一个包含输入数组的表单,并且在发生验证错误时我很难显示翻译后的属性名称。为简单起见,我将发布一个简单的问题示例。

视图内的表单:

<form method="POST" action="{{ route('photo.store') }}" accept-charset="UTF-8" role="form">
<input name="_token" type="hidden" value="{{ csrf_token() }}">
<div class="row">
    <div class="col-lg-12">
        <div class="form-group{{ $errors->has('testfield') ? ' has-error' : '' }}">
            <label class="control-label"
                   for="testfield">{{ trans('validation.attributes.testfield') }}</label>
            <input class="form-control" name="testfield" type="text" id="testfield"
                   value="{{ old('testfield') }}">
            @if ($errors->has('testfield'))
                <p class="help-block">{{ $errors->first('testfield') }}</p>
            @endif
        </div>
    </div>
    <div class="col-lg-12">
        <div class="form-group{{ $errors->has('testfieldarray.0') ? ' has-error' : '' }}">
            <label class="control-label"
                   for="testfieldarray-0">{{ trans('validation.attributes.testfieldarray') }}</label>
            <input class="form-control" name="testfieldarray[]" type="text" id="testfieldarray-0"
                   value="{{ old('testfieldarray.0') }}">
            @if ($errors->has('testfieldarray.0'))
                <p class="help-block">{{ $errors->first('testfieldarray.0') }}</p>
            @endif
        </div>
    </div>
    <div class="col-lg-12">
        <div class="form-group{{ $errors->has('testfieldarray.1') ? ' has-error' : '' }}">
            <label class="control-label"
                   for="testfieldarray-1">{{ trans('validation.attributes.testfieldarray') }}</label>
            <input class="form-control" name="testfieldarray[]" type="text" id="testfieldarray-1"
                   value="{{ old('testfieldarray.1') }}">
            @if ($errors->has('testfieldarray.1'))
                <p class="help-block">{{ $errors->first('testfieldarray.1') }}</p>
            @endif
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <input class="btn btn-primary" type="submit" value="Gravar">
    </div>
</div>

表单请求中的规则功能:

public function rules() {
    $rules = [
        'testfield' => array('required'),
    ];

    foreach ($this->request->get('testfieldarray') as $key => $val) {
        $rules['testfieldarray.' . $key] = array('required');
    }

    return $rules;
}

lang/en/validation.php

'attributes' => [
    'testfield' => 'Test Field',
    'testfieldarray' => 'Test Field Array',
],

验证正确执行,错误消息也是如此。错误消息中的唯一问题是显示的属性名称。在两个数组输入中,插入到消息中的属性名称是“testfieldarray.0”和“testfieldarray.1”,而不是“Test Field Array”。我已经尝试添加语言文件 'testfieldarray.0' => 'Test Field Array', 'testfieldarray.1' => 'Test Field Array',但消息保持不变。有没有办法正确传递属性名称?

4

3 回答 3

1

1-如果您在文件请求中拆分验证,则添加方法属性并设置每个键的值,如下所示:

 public function attributes()
    {
        return [
          'name'=>'title',
         

        ];
    }

2-但如果不拆分请求的验证,那么您只需要创建变量属性并传递项目的值,如下所示:

$rules = [
  'account_number' => ['required','digits:10','max:10','unique:bank_details']
];
$messages = [];
$attributes = [
    'account_number' => 'Mobile number',
];
$request->validate($rules,$messages,$attributes);
// OR
$validator = Validator::make($request->all(), $rules, $messages, $attributes);
于 2022-03-01T11:49:45.610 回答
1

只需查看示例为数组的整数类型值检查添加自定义规则

打开文件

/resources/lang/en/validation.php

然后添加自定义消息。

// add it before "accepted" message.
'numericarray'         => 'The :attribute must be numeric array value.',

再次打开另一个文件以添加自定义验证规则。

/app/Providers/AppServiceProvider.php

因此,在 boot 函数中添加自定义验证码。

public function boot()
{
    $this->app['validator']->extend('numericarray', function ($attribute, $value, $parameters)
    {
        foreach ($value as $v) {
            if (!is_int($v)) {
                return false;
            }
        }
        return true;
    });
}

现在您可以使用numericarray进行数组的整数类型值检查。

$this->validate($request, [
            'input_filed_1' => 'required',
            'input_filed_2' => 'numericarray'
        ]);

------------祝你好运----------------

于 2016-09-05T09:55:27.767 回答
0

在您的父方法中使用自定义错误消息....

public function <metod>(Request $request) {
    $rules = [
        'testfield' => 'required'
    ];
    $messages = [];

    foreach ($request->input('testfieldarray') as $key => $val) {
        $rules['testfieldarray.' . $key] = 'required';
        $messages['testfieldarray.' . $key . '.required'] = 'Test field '.$key.' is required';
    }

    $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            $request->flash();
            return redirect()
                ->back()
                ->withInput()
                ->withErrors($validator);
        }
    }
}
于 2015-11-17T04:01:51.360 回答