欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Laravel5 表单验证问题

程序员文章站 2024-01-19 22:40:34
...

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

如图所示:
设置了

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

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

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

Laravel5 表单验证问题

貌似图片挂了,截图: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

回复内容:

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

如图所示:
设置了

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

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

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

Laravel5 表单验证问题

貌似图片挂了,截图: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

'custom' => [
        'title' => [
            'required' => '标题不能为空',
        ],
    ],

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

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