自定义请求未在表单验证成功时调用控制器方法 [英] Custom request not calling controller method on form validation success
问题描述
我正在尝试在Laravel 5上使用自定义请求来验证通过邮寄发送的输入,但是当表单字段可用时,即.表单验证进行得很好,未调用我的控制器的方法.那是个错误吗?怎么了?我知道可能是因为我以前在另一个项目中这样做过.我在该项目中使用的是5.2.6版本,然后我认为这是该特定版本的问题,那就是当我切换回"至5.1但编辑我的composer.json
,删除vendor
文件夹然后使用
I'm trying to use a custom request on Laravel 5 to validate inputs sent through post, but when the form fields are ok, ie. form validation goes fine, my controller's method is not called. Is that a bug? What's wrong? I know it's possible 'cause I did this once ago in another project. I was using version 5.2.6 in this project, then I thought It was a issue with this specific version, that's when I "switched back" to 5.1 but editing my composer.json
, deleting vendor
folder and then reinstalling with composer install
.
我的UserController的内容:
Contents of my UserController:
//...
public function save(Requests\CreateUserRequest $request)
{
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->username = $request->input('username');
$user->password = $request->input('password_confirmation');
//$user->profile_id = $request->input('profile');
$user->profile_id = 1;
$user->save();
return redirect()->route('get_user_read');
}
//...
CreateUserRequest:
CreateUserRequest:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateUserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'profile' => 'required',
'name' => 'required',
'username' => 'required|unique:users,username',
'email' => 'required|email|unique:users,email',
'password' => 'required',
'password_confirmation' => 'required|same:password',
];
}
public function messages()
{
return [
'profile.required' => 'O campo perfil é obrigatório!',
'name.required' => 'O campo nome é obrigatório!',
'username.required' => 'O campo nome de usuário é obrigatório!',
'username.unique' => 'O nome de usuário já foi registrado!',
'email.required' => 'O campo email é obrigatório!',
'email.unique' => 'O email informado já foi registrado!',
'email.email' => 'O email informado não é válido!',
'password.required' => 'O campo senha é obrigatório!',
'password_confirmation.required' => 'O campo de confirmação de senha é obrigatório!',
'password_confirmation.same' => 'A confirmação de senha não confere com a senha informada!',
];
}
}
谢谢.
推荐答案
从Laravel 5.2开始,有一些功能称为中间件组.
Since Laravel 5.2 there is some feature called Middleware Groups.
由于FAILED验证,似乎可以将您重定向回表单,而不会显示任何错误.在会话中启用共享之前,您不会看到任何错误.
It seems it gets you redirected back to your form because of FAILED validation without showing any errors. You would not see any errors until you enable sharing them within a session.
要使错误可见,请确保已启用\Illuminate\View\Middleware\ShareErrorsFromSession::class
中间件.默认情况下,它包含在"Web"中间件组中.
To make errors visible please ensure that \Illuminate\View\Middleware\ShareErrorsFromSession::class
Middleware is enabled. By default it's included to "web" middleware group.
要解决您的问题,您可以简单地创建一个路由组并将中间件组设置为"web",如下所示:
To solve your issue you can simply create a route group and set a middleware group to "web" like this:
Route::group(['middleware' => ['web']], function () {
// Here comes your routes
});
请查看以下内容: Laravel 5.2验证错误
还要确保您的表单中有刀片错误标签,如下所示:
Also ensure you have a blade error tags in your form like this:
{!! Form::text('profile') !!}
@if ($errors->has('profile'))<p style="color:red;">{!!$errors->first('profile')!!}</p>@endif
{!! Form::text('name') !!}
@if ($errors->has('name'))<p style="color:red;">{!!$errors->first('name')!!}</p>@endif
或者您可以显示所有这些内容:
Or you can display all of them:
@if($errors->any())
<div style="color:red;">
@foreach($errors->all() as $error)
<p>{{$error}}</p>
@endforeach
</div>
@endif
此外,您可能不使用public function messages()
方法,而是使用resources/lang/pt/validation.php
文件为您的语言定义了所有消息.
Also you may not use public function messages()
method and define all the messages for your language using resources/lang/pt/validation.php
file.
这篇关于自定义请求未在表单验证成功时调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!