如何在输入数组上使用自定义验证属性

时间:2015-05-21 11:29:27

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

我正在使用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;
}

郎/ EN / validation.php

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

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

2 个答案:

答案 0 :(得分:1)

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

打开文件

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

----------- 最好的运气 --------------

答案 1 :(得分:0)

在父方法中使用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);
        }
    }
}