Laravel5 表单验证问题
为什么设置了表单验证的attributes仍然不显示其设定的值?
如图所示:
设置了
<code>$attributes = array( "title" => '标题', 'content' => '正文' );</code>
提示仍然是title 不能小于10个字
而不是标题 不能小于10个字
有用过的大神知道是怎么回事没。
貌似图片挂了,截图: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>
视图:
<code>@extends('app') @section('content') <div class="container"> <h1 id="Article">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>
回复内容:
为什么设置了表单验证的attributes仍然不显示其设定的值?
如图所示:
设置了
<code>$attributes = array( "title" => '标题', 'content' => '正文' );</code>
提示仍然是title 不能小于10个字
而不是标题 不能小于10个字
有用过的大神知道是怎么回事没。
貌似图片挂了,截图: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>
视图:
<code>@extends('app') @section('content') <div class="container"> <h1 id="Article">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>
<code>'custom' => [ 'title' => [ 'required' => '标题不能为空', ], ],</code>
找到resources/lang/en/validation.php
中的custome,这个满足你的要求不?更多的Laravel 5教程:
https://laravist.com/series/laravel-5-basic

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
