為什麼設定了表單驗證的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