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

9

12

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!

Lanalanae answered 23/9, 2014 at 20:49 Comment(4)
Having same issue in Laravel 5 and don't know why...Harbaugh
Me too! In Laravel 5, but only with certain environments / serversSearching
@Harbaugh Quite sad, actually put me off Laravel... Not bad because I am learning more Javascript now :) If you found an answer please post it though?Lanalanae
@IndigoIdentity My problem was I overwrote the default settings with Config::set('app', $my_config_array) and because of that I lose the app.language value.Harbaugh
S
10

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.

Slash answered 23/9, 2014 at 21:17 Comment(1)
Hey, thanks for the reply. I am using the default error messages which are indeed defined in the validation.php file. Really odd, everything seems to be 100% correct but the validation is not translating the errors into messages?Lanalanae
F
7

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

Frantz answered 10/5, 2015 at 17:50 Comment(0)
S
4

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.

Summerville answered 17/8, 2017 at 7:44 Comment(0)
S
2

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.

Sc answered 30/10, 2015 at 23:9 Comment(0)
M
0

Solution for this problem check your lang directory exits or not under /resources directory.

Macleod answered 30/10, 2020 at 11:1 Comment(0)
S
0

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

Salinger answered 2/1, 2021 at 5:21 Comment(0)
B
0

File project\lang\en\validation.php is missing in project\resources\lang\en\validation.php.

SIMPLE: lang folder it not exists in resources\lang. Just copy paste lang folder in resources.

How I figure it out? I install a new laravel app with breez Vue Inertia and it worked fine with errors messages, then I compare my current project structure and do some research and finally got the solution.

Just copy whole folder from project\lang and paste in project\resources\lang. If there is no lang folder in resources then simply create it and paste copied files. And its worked! This error mostly happened when you create simple Laravel project and later install Vue etc. and during installation/configuration the lang folder may skip accidentally in resources/lang.

If you have no validation.php file, then simply goto github laravel/laravel and copy that file.

Brinker answered 7/12, 2022 at 16:24 Comment(0)
H
0

You need to create a file at

resources/lang/en/validation.php

And copy content from this url PHPClasses

Harsho answered 4/4, 2023 at 11:30 Comment(2)
It's still not understandable why would he do this, there's no enough informationSupersede
Because these steps fixed my problem and now it's showing the actual error rather validation.required, it's nothing but only two steps, one create a file named validation.php at the specified location and two, copy content from the PHPClassesHarsho
T
0

I had same issue.

I replaced the resources folder with my older one where there was no lang/en/validation.php and that was the cause, you just have to add the file!!

Tamtama answered 15/3 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.