0

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.

Serhii Matrunchyk
  • 9,083
  • 6
  • 34
  • 47
Anderson Madeira
  • 420
  • 9
  • 29
  • Please check if " Requests\CreateUserRequest " is the real path to your request file – BKF Feb 11 '16 at 20:07
  • So if you dd() your request in the save function nothing happens? – Mike Miller Feb 11 '16 at 20:31
  • To test if my save function is ever called i put ```return " ";``` before everything else; and it's never executed. I should see a blank page. As I said above, when I use my custom request to validade form inputs, my save function is NEVER called. – Anderson Madeira Feb 11 '16 at 22:23
  • Does your form has all these values described in array of `rules` method? I.e. profile, name, username, email, password, password_confirmation. Check if it works when you change your `rules` method with `return []` – Serhii Matrunchyk Feb 12 '16 at 02:35
  • Did answered correctly for your question? – Serhii Matrunchyk Feb 15 '16 at 14:27

1 Answers1

0

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.

Community
  • 1
  • 1
Serhii Matrunchyk
  • 9,083
  • 6
  • 34
  • 47