Laravel - 错误消息未转换为实际错误消息,以“验证”开头。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26004492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:09:13  来源:igfitidea点击:

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

phpvalidationlaravellaravel-4localization

提问by Craig van Tonder

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

我在尝试在 Laravel 中进行表单验证时遇到问题,这很奇怪,因为它通常可以正常工作。

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!

如果有人可以建议为什么会发生这种情况或我缺少什么,那么将不胜感激!

回答by Alley Shairu

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

如果它之前对您有用,那么您应该检查您是否在 中定义了消息,app\lang\en\validation.php或者您更改了应用程序的区域设置并且没有为其定义消息。有很多可能性。

回答by Sorin Vladu

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

我有一个类似的问题,我的问题是我不小心删除了 resources/lang/en/validation.php

回答by Markus Tenghamn

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.

当我将语言环境和备份语言环境都设置为在 app.php 中没有验证文件的语言环境时,我遇到了类似的问题。尝试将您的备份语言环境设置为 en 或您确定存在的语言环境。

回答by Rittichai Rungpoonsap

I had that issue. Check the langdirectory, it should be placed in the resourcedirectory.

我有那个问题。检查lang目录,它应该放在resource目录中。

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.

我将 lang 目录拖到另一个目录,然后我得到了与您相同的验证响应。修复只是将其移回资源目录。