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

时间:2014-09-23 20:49:35

标签: php validation laravel laravel-4 localization

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

在我的用户模型中,我创建了验证规则:

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

在我的用户控制器中,我仅在验证通过时定义操作,否则用户被重定向并返回错误:

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

发生了以下错误:

在视图中,如果存在错误,则显示错误:

@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

我遇到的问题是提交空表单时的输出是:

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

而不是:

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.

如果有人能说明为什么会发生这种情况或者我缺少什么,那么我们将不胜感激!

6 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:5)

我有一个类似的问题,问题是我不小心删除了 资源/郎/ EN / validation.php

答案 2 :(得分:3)

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.

答案 3 :(得分:3)

我遇到了这个问题。 检查lang目录,它应放在resource目录中。

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

答案 4 :(得分:1)

我有同样的问题,这里是这个问题的解决方案

检查resources/lang/en/validation.php文件是否存在

在 config\app.php 中将您的语言环境和 fallback_locale 更改为“en”

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

然后运行,php artisan config:cache

答案 5 :(得分:0)

此问题的解决方案请检查您的lang目录是否退出,而不在/resources目录下。