Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Error messages not translating to actual error message, start with "validation."

I am having issues trying to get form validation working in Laravel which is odd because it usually just works.

In my user model I have created rules for the validation:

public static $rules = array(
    'firstname'=>'required|min:2|alpha',
    'lastname'=>'required|min:2|alpha',
    'email'=>'required|email|unique:users',
    'password'=>'required|alpha_num|between:8,12|confirmed',
    'password_confirmation'=>'required|alpha_num|between:8,12',
    'telephone'=>'required|between:10,12',
    'admin'=>'integer'
);

In my user controller I define actions only if the validation passes, if not the user is redirected back with errors:

public function postCreate() {
    $validator = Validator::make(Input::all(), User::$rules);

    if ($validator->passes()) {
        $user = new User;
        $user->firstname = Input::get('firstname');
        $user->lastname = Input::get('lastname');
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));
        $user->telephone = Input::get('telephone');
        $user->save();

        return Redirect::to('users/signin')
            ->with('message', 'Thank you for creating a new account. Please sign in.');
    }

    return Redirect::to('users/create-account')
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

The following errors have occurred:

In the view i display the errors if they exist:

@if($errors->has())
    <div id="form-errors">
    <p>The following errors have occurred:</p>

    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
    </div><!-- end form-errors -->
@endif

The problem that I have is that the output of this when submitting an empty form is:

validation.required
validation.required
validation.required
validation.required
validation.required
validation.required

Instead of:

The firstname field is required.
The lastname field is required.
The email field is required.
The password field is required.
The password confirmation field is required.
The telephone field is required.

If anyone could suggest why this is happening or what I am missing then this would be greatly appreciated!

like image 230
Craig van Tonder Avatar asked Sep 23 '14 20:09

Craig van Tonder


5 Answers

I had that issue. Check the lang directory, it should be placed in the resource directory.

I dragged lang directory to another directory, and then I got the same validation response as you. The fix was just to move it back to resource directory.

like image 111
Rittichai Rungpoonsap Avatar answered Nov 19 '22 05:11

Rittichai Rungpoonsap


I have the same problem, here is the solution for this problem

Check whether resources/lang/en/validation.php file exists or not

or

Change your locale and fallback_locale to 'en' in config\app.php

'locale' => 'en',
...
...
'fallback_locale' => 'en',
...

then run, php artisan config:cache

like image 32
Bauroziq Avatar answered Sep 28 '22 02:09

Bauroziq


I had a similar issue, and the problem to me was that I've accidentally deleted resources/lang/en/validation.php

like image 8
Sorin Vladu Avatar answered Nov 19 '22 06:11

Sorin Vladu


If it has worked for you before then you should check if you have messages defined in the app\lang\en\validation.php or by chance you have changed the locale of the app and have not defined the messages for it. There are many possibilities.

like image 10
Alley Shairu Avatar answered Nov 19 '22 05:11

Alley Shairu


I had a similar problem when I had set both my locale and backup locale to a locale that had no validation file in app.php. Try setting your backup locale to en or one that you are sure exists.

like image 3
Markus Tenghamn Avatar answered Nov 19 '22 05:11

Markus Tenghamn