素材牛VIP会员
Laravel5 表单验证问题
 随***火  分类:PHP代码  人气:1159  回帖:2  发布于5年前 收藏

为什么设置了表单验证的attributes仍然不显示其设定的值?

如图所示:
设置了

$attributes = array(
            "title" => '标题',
            'content' => '正文'
        );

提示仍然是title 不能小于10个字 而不是标题 不能小于10个字

有用过的大神知道是怎么回事没。

图片描述

貌似图片挂了,截图:http://7xjkan.com1.z0.glb.clouddn.com/QQ%E5%9B%BE%E7%89%8720151026170220.png

controller:

    public function update(Request $request,$id)
    {

        $rules = array(
            'title' => 'required|max:255|min:10',
            'content' => 'required|min:50|max:20000',
        );
        $message = array(
            "title.required"=> ":attribute 不能为空",
            "title.min"=> ":attribute 不能小于10个字",
            "title.max"=> ":attribute 不能超过50个字",
            "content.required"=>":attribute 不能为空",
            "content.min"=>":attribute 不能小于50个字",
            "content.max"=>":attribute 不能超过20000个字"
        );

        $attributes = array(
            "title" => '标题',
            'content' => '正文'
        );

        /*$this->validate($request, [
            'title' => 'required|max:255|min:5',
            'content' => 'required',
        ]);*/
        $this->validate($request,$rules,$message,$attributes);
        $article = Article::find($id);
        $article->title = \Input::get('title');
        $article->content = \Input::get('content');

        if ($article->save()) {
            return \Redirect::to('article');
        } else {
            return \Redirect::back()->withInput()->withErrors('保存失败!');
        }
    }

视图:

@extends('app')

@section('content')
<div class="container">
<h1 class="page-header text-center">Article</h1>
<div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4">
    @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

        <form action="{{ URL('article/'.$article->id) }}" method="POST">
            <input name="_method" type="hidden" value="PUT">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <input type="text" name="title" class="form-control" required="required" value="{{ $article->title }}">
            <br>
            <textarea name="content" rows="10" class="form-control" required="required">{{ $article->content }}</textarea>
            <br>
            <button class="btn btn-lg btn-info">编辑 Page</button>
        </form>

    </div>
    <div class="col-md-4"></div>

</div>
</div>
@stop
 标签:phplaravel

讨论这个帖子(2)垃圾回帖将一律封号处理……

Lv1 新人
冷***知 职业无 5年前#1
$this - > validate($request, [
        'title' = > 'required|integer|between:1,5',
], [
        'title.required' = > 'title必填',
        'title.between' = > 'title字数必须在1到5个之间',
]);

亲测可用

Lv4 码徒
静***5 Linux系统工程师 5年前#2
'custom' => [
        'title' => [
            'required' => '标题不能为空',
        ],
    ],

找到resources/lang/en/validation.php中的custome,这个满足你的要求不?更多的Laravel 5教程:

https://laravist.com/series/laravel-5-basic

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取