首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel 5请求-如何显示单个验证错误

Laravel 5请求-如何显示单个验证错误
EN

Stack Overflow用户
提问于 2015-04-05 18:47:02
回答 3查看 12.7K关注 0票数 2

我在L5中创建了一个保存联系人的请求,如下所示:

代码语言:javascript
运行
复制
<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class AddContactRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'category_ids' => 'required|array|exists:categories,id',
            'gender' => 'required|in:Male,Female',
            'first_name' => 'required',
            'last_name' => 'required',
            'company' => 'required',
            'position' => 'required',
            'website_url' => 'url',
            'facebook_url' => 'url',
            'twitter_url' => 'url',
            'instagram_url' => 'url',
            'misc_url1' => 'url',
            'misc_url2' => 'url',
            'address_1' => 'required',
            'citytown' => 'required',
            'postcode' => 'required',
            'country_id' => 'required|integer|exists:countries,id',
            'work_email' => 'email',
            'home_email' => 'email',
            'work_phone' => '',
            'mobile_phone' => '',
            'home_phone' => ''
        ];
    }

    /**
     * Error messages for validation rules
     * @return array
     */
    public function messages()
    {
        return [
            'category_ids.required' => 'You must select at least one category.',
            'category_ids.array' => 'Categories must be an array.',
            'category_ids.exists' => 'The category does not exist.',
            'gender.required' => 'The gender field is required.',
            'gender.in' => 'The gender is invalid.',
            'first_name.required' => 'The first name field is required.',
            'last_name.required' => 'The last name field is required.',
            'company.required' => 'The company field is required.',
            'position.required' => 'The position field is required.',
            'website_url.url' => 'The website URL is not a valid URL.',
            'facebook_url.url' => 'The facebook URL is not a valid URL.',
            'twitter_url.url' => 'The twitter URL is not a valid URL.',
            'instagram_url.url' => 'The instagram URL is not a valid URL.',
            'misc_url1.url' => 'The miscellaneous URL 1 field is not a valid URL',
            'misc_url2.url' => 'The miscellaneous URL 2 field is not a valid URL',
            'address_1.required' => 'The address 1 field is required.',
            'citytown.required' => 'The city / town field is required.',
            'postcode.required' => 'The postcode field is required.',
            'country_id.required' => 'The country field is required.',
            'country_id.integer' => 'The country field must contain an integer.',
            'country_id.exists' => 'The country is invalid.',
            'work_email.email' => 'The work email field is not a valid email address.',
            'home_email.email' => 'The home email field is not a valid email address.'
        ];
    }

}

当验证失败时,我可以使用以下代码输出视图中的所有错误:

代码语言:javascript
运行
复制
@if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

但我希望显示单个错误,并在该字段出现错误时向表单组中添加一个类,如下所示:

代码语言:javascript
运行
复制
<div class="form-group <?php echo ($messages->has('first_name')) ? 'has-error' : ''; ?>">
    <label for="first_name" class="control-label col-xs-12 col-sm-3 col-md-4 col-lg-4">First Name</label>
    <div class="col-xs-12 col-sm-6 col-md-8 col-lg-8">
        {!! Form::text('first_name', '', array('class' => 'form-control')) !!}
    </div>
</div>

$messages->has()方法似乎不起作用,尽管我得到了以下错误:

代码语言:javascript
运行
复制
Whoops, looks like something went wrong.

2/2
ErrorException in 6351f2a902813f4fd7aa70b7ef38354d line 34:
Undefined variable: messages (View: C:\xampp\htdocs\fourteen-ten\resources\views\admin\contacts\add.blade.php)

有人知道我为什么会收到这个错误吗?

EN

Stack Overflow用户

回答已采纳

发布于 2015-04-05 18:54:13

好的,多亏了苏珊,我才能弄明白。而不是:

代码语言:javascript
运行
复制
$messages->has('field_name')

我应该使用:

代码语言:javascript
运行
复制
$errors->has('field_name')
票数 3
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29456483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档