laravel表单自定义验证提示信息

水一篇 有用的文章
laravel的验证器中,默认的提示信息是英文,但在实际开发中,经常会使用中文的提示信息,尤其是在验证表单输入时。
  由于官方文档写得很是简略,所以当时在使用验证器自定义提示信息时,还是很花了一番功夫,网上也查了一些资料,但都是神龙见首不见尾,因此在此记录下,让初学者少走弯路。
  自定义验证器的内容如下,相信只要有点PHP功底的,都能看懂,看不懂的自己去补PHP基础。

public function store(Request $request)
{
    //定义验证规则,是一个数组
    $rules = [
        'title' => 'required|max:64',
        'content' => 'required|max:500',
        'back_color' => 'required|max:10',
        'user_name' => 'required|max:10',
    ];

    //定义提示信息
    $messages = [
        'title.required' => '留言标题不能为空',
        'content.required' => '留言内容不能为空',
        'back_color.required' => '留言背景颜色不能为空',
        'user_name.required' => '留言者姓名不能为空',
    ];

    //创建验证器
    $validator = Validator::make($request->all(), $rules, $messages);
    if ($validator->fails()) {
        return redirect('/index')
                    ->withErrors($validator)
                    ->withInput();
    }

    $posts = Posts::create($request->all());

    return redirect('/index');

}

    

  虽然定义好了验证消息,但是怎么在模板中使用自定义的验证消息,卡了我很久,看到官方的common.errors中输出验证消息的源代码,修改如下以显示索引信息:

@if (count($errors) > 0)
    <!-- Form Error List -->
    <div class="alert alert-danger">
        <strong>Whoops! Something went wrong!</strong>

        <br><br>

        <ul>
            @foreach ($errors->all() as $k => $error)
                <li>{{ $k }} => {{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

   
  发现他是一个索引数组,于是想在表单中直接引用,但是直接使用索引输出会出现问题,使用索引输出的代码如下:

$errors->all()[0];//输出第一个提示信息


  使用这种方法,直接将提示信息写死了,假设表单中有5条信息,那么就应该有5个验证消息,用户有多少个信息没有填,就会产生多少条提示信息,如果直接使用索引数组下标的方式输出,程序无法判断使用错误提示信息中的哪一个索引。

  正确的引用提示消息的代码如下:

<form action="{{ url('store') }}" method="POST" class="form-horizontal">
 {{ csrf_field() }}

    <fieldset>
        <div class="fhint">
            <label id="fhint">添加文章</label>
        </div>

        <div class="ftitle">
            <label></label>
            <input>
            @if ($errors->has('title'))
                <label></label>
                <p style="display: inline; background-color: red">{{ $errors->first('title') }}</p >
            @endif
        </div>
        <div class="fcontent">
            <label </label>
            <textarea></textarea>
            @if ($errors->has('content'))
                <label></label>
                <p style="display: inline; background-color: red">{{ $errors->first('content') }}</p >
            @endif
        </div>
        <div class="fbackcolor">
            <label></label>
            <select name="back_color" id="fbackcolor" onchange="checkValue(event);">
                <option selected="true" disabled="disabled" value>请选择黄色或紫色</option>
                <option value="yellow">黄色</option>
                <option value="#ff00ff">紫色</option>
            </select>
                @if ($errors->has('back_color'))
                    <label></label>
                    <p style="display: inline; background-color: red">{{ $errors->first('back_color') }}</p >
                @endif
        </div>

        <div class="fname">
            <label for="fname">名字</label>
            <input type="text" id="fname" name="user_name" onkeyup="checkValue(event);">
            @if ($errors->has('user_name'))
                <label></label>
                <p style="display: inline; background-color: red">{{ $errors->first('user_name') }}</p >
            @endif
        </div>
        <div class="fsubmit">
            <label></label>
            <input type="submit" value="提交" id="submit">
        </div>
    </fieldset>
</form>
--------------------- 

来源:CSDN
原文:https://blog.csdn.net/GorgeousChou/article/details/80275159

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原文链接 必备品 文档:Documentation API:API Reference 视频:Laracasts ...
    layjoy阅读 12,729评论 0 121
  • Laravel 学习交流 QQ 群:375462817 本文档前言Laravel 文档写的很好,只是新手看起来会有...
    Leonzai阅读 12,713评论 2 12
  • 1、简介 Laravel 提供了多种方法来验证应用输入数据。默认情况下,Laravel 的控制器基类使用Valid...
    伊Summer阅读 5,453评论 0 3
  • 验证 简介 Laravel 对验证应用的输入数据提供了多中途径的实现。默认的,Laravel 的基础控制器类使用了...
    Dearmadman阅读 13,466评论 5 8
  • 《定風波-美玉蘭花》 别時尚早满樹碧, 玉蘭淡红春來急。 瓣似新燕繁如雨, 花期, 婆娑昨夜東風泣。 箭袖舞罢蛮腰...
    独行彡阅读 2,118评论 0 0

友情链接更多精彩内容