gpt4 book ai didi

php - Laravel - 错误消息未转换为实际错误消息,以 "validation."开头

转载 作者:可可西里 更新时间:2023-11-01 12:27:42 25 4
gpt4 key购买 nike

我在尝试在 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'
);

在我的用户 Controller 中,我只在验证通过时定义操作,否则用户将被重定向回错误:

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

发生了以下错误:

如果错误存在,我会在 View 中显示错误:

@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.

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

最佳答案

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

关于php - Laravel - 错误消息未转换为实际错误消息,以 "validation."开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26004492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com