php - Laravel5 表单验证问题
巴扎黑
巴扎黑 2017-04-10 16:12:38
[PHP讨论组]

为什么设置了表单验证的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')

Article

@if (count($errors) > 0)

Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach

@endif


@stop
巴扎黑
巴扎黑

全部回复(1)
ringa_lee
$this - > validate($request, [
        'title' = > 'required|integer|between:1,5',
], [
        'title.required' = > 'title必填',
        'title.between' = > 'title字数必须在1到5个之间',
]);

亲测可用

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号