Home > Backend Development > PHP Tutorial > Laravel5 表单验证问题

Laravel5 表单验证问题

WBOY
Release: 2016-06-06 20:25:57
Original
1122 people have browsed it

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

如图所示:
设置了

<code>$attributes = array(
            "title" => '标题',
            'content' => '正文'
        );</code>
Copy after login
Copy after login

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

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

Laravel5 表单验证问题

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

controller:

<code>    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('保存失败!');
        }
    }</code>
Copy after login
Copy after login

视图:

<code>@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="%7B%7B%20URL('article/'.%24article->id)%20%7D%7D" 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</code>
Copy after login
Copy after login

回复内容:

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

如图所示:
设置了

<code>$attributes = array(
            "title" => '标题',
            'content' => '正文'
        );</code>
Copy after login
Copy after login

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

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

Laravel5 表单验证问题

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

controller:

<code>    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('保存失败!');
        }
    }</code>
Copy after login
Copy after login

视图:

<code>@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="%7B%7B%20URL('article/'.%24article->id)%20%7D%7D" 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</code>
Copy after login
Copy after login

<code>'custom' => [
        'title' => [
            'required' => '标题不能为空',
        ],
    ],</code>
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template