Laravel:某些字段的自定义验证不起作用

时间:2017-07-18 05:14:01

标签: php laravel validation laravel-5.4

在我的应用程序中,地址字段的自定义验证无效,但其他文件正在运行。不知道这个问题。

查看

<div class="col-md-4">
    <label for="email">Location:</label>
    <div class="form-group">
        <input name="address" id="autocomplete" placeholder="Enter your address"
         onFocus="geolocate()" class="form-control" value="{!!$company->address!!}" type="text"></input>
    </div>
</div>

控制器

$this->validate($request, [
    'name' => 'required|regex:/^[a-zA-Z .0-9]+$/',
    'office_mail' => 'required|email|max:255',
    'address' => 'required|regex:/^[a-zA-Z ,0-9]+$/',
    'industry_id' => 'required',
    'contact_number' => 'numeric|digits_between:10,12',
    'company_website_url' => 'required|url',
]);

Validation.php

'custom' => [
    'name' => [
        'required' => 'Your name is required',
    ],
    'address' => [
        'required' => 'The location field is required',
    ],
    'address' => [
        'regex' => 'Please enter a valid location',
    ],
    'company_id' => [
        'required' => 'The company field is required',
    ],
]

名称的自定义验证工作正常,我收到“您的名字是必需的”验证消息。但是对于地址字段我得到'地址字段是必需的'而不是'需要位置字段'

2 个答案:

答案 0 :(得分:5)

'custom' => [
'name' => [
    'required' => 'Your name is required',
],
'address' => [
    'required' => 'The location field is required',
    'regex' => 'Please enter a valid location',
],
'company_id' => [
    'required' => 'The company field is required',
]
]

答案 1 :(得分:2)

  

因为你的定义地址字段在数组中是2次所以第1次覆盖   到第二个。

在第二个数组中没有所需字段的消息,这是其显示默认消息的原因

'address' => [
    'regex' => 'Please enter a valid location', // No message for required
],

需要将其更改为:

'address' => [
    'required' => 'The location field is required',
    'regex' => 'Please enter a valid location',
],