Custom request not calling controller method on form validation success

谁说我不能喝 提交于 2019-12-12 03:34:57

问题


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.

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:

<?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!',
    ];
}
}

Thanks.


回答1:


Since Laravel 5.2 there is some feature called Middleware Groups.

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.

To make errors visible please ensure that \Illuminate\View\Middleware\ShareErrorsFromSession::class Middleware is enabled. By default it's included to "web" middleware group.

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
});

Please take a look on this: Laravel 5.2 validation errors

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

Also you may not use public function messages() method and define all the messages for your language using resources/lang/pt/validation.php file.



来源:https://stackoverflow.com/questions/35348942/custom-request-not-calling-controller-method-on-form-validation-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!