PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Laravel5 表单验证问题

原创
2016-06-06 20:25:57 834浏览

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

回复内容:

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

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

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

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。