【问题标题】:How to use custom validation attributes on an array of inputs如何在输入数组上使用自定义验证属性
【发布时间】:2015-08-03 01:04:02
【问题描述】:

我正在使用 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”,而不是“测试字段数组”。我已经尝试添加语言文件 'testfieldarray.0' => 'Test Field Array', 'testfieldarray.1' => 'Test Field Array',但消息保持不变。有没有办法正确传递属性名?

【问题讨论】:

  • 您可以循环遍历验证中自定义消息数组的每个“testfieldarray”,就像您使用规则一样。谷歌搜索 1 秒后ericlbarnes.com/laravel-array-validation
  • 你不应该在'testfieldarray.' . $key之前添加attributes.

标签: laravel laravel-5 laravel-validation laravel-request


【解决方案1】:

在你的父方法中使用custom error messages....

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);
        }
    }
}

【讨论】:

    【解决方案2】:

    添加自定义数组整型值检查规则见示例

    打开文件

    /resources/lang/en/validation.php
    

    然后添加自定义消息。

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

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

    /app/Providers/AppServiceProvider.php
    

    所以,在启动函数中添加自定义验证码。

    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'
            ]);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 2020-04-18
      • 2016-04-03
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多