Laravel 使验证请求无效
Laravel make validation request not working
当我提交表单时没有任何反应。
这是我的步骤。
php artisan make:request PostRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title'=>['required','max:255'],
'article'=>['required'],
'image'=>['image']
];
}
public function messages()
{
return [
'title.required'=>['need title'],
'article.required'=>['need article'],
];
}
}
?>
App/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests\PostRequest;
use Illuminate\Support\Facades\Auth;
class PostController extends Controller
{
public function store(PostRequest $request)
{
Post::create($request->all());
return redirect()->route('post.index');
}
}
?>
如果我不使用 PostRequest,它会像这样完美地工作。
app/Http/Controllers/PostController.php
public function store(Request $request)
{
$post=new Post;
$post->user_id=Auth::user()->id;
$post->title=$request->title;
$post->article=$request->article;
$post->image=$request->image;
$post->save();
return redirect()->route('post.index');
}
我错过了什么步骤?
谢谢大家。
您的 rules()
函数有一个 return 数组。
public function rules()
{
return [
'title'=>['required','max:255'],
'article'=>['required'],
'image'=>['image']
];
}
改为
public function rules()
{
return [
'title'=>'required|max:255',
'article'=>'required',
'image'=>'image'
];
}
当我提交表单时没有任何反应。
这是我的步骤。
php artisan make:request PostRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title'=>['required','max:255'],
'article'=>['required'],
'image'=>['image']
];
}
public function messages()
{
return [
'title.required'=>['need title'],
'article.required'=>['need article'],
];
}
}
?>
App/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests\PostRequest;
use Illuminate\Support\Facades\Auth;
class PostController extends Controller
{
public function store(PostRequest $request)
{
Post::create($request->all());
return redirect()->route('post.index');
}
}
?>
如果我不使用 PostRequest,它会像这样完美地工作。
app/Http/Controllers/PostController.php
public function store(Request $request)
{
$post=new Post;
$post->user_id=Auth::user()->id;
$post->title=$request->title;
$post->article=$request->article;
$post->image=$request->image;
$post->save();
return redirect()->route('post.index');
}
我错过了什么步骤? 谢谢大家。
您的 rules()
函数有一个 return 数组。
public function rules()
{
return [
'title'=>['required','max:255'],
'article'=>['required'],
'image'=>['image']
];
}
改为
public function rules()
{
return [
'title'=>'required|max:255',
'article'=>'required',
'image'=>'image'
];
}