【发布时间】:2015-08-28 20:50:56
【问题描述】:
我目前正在我的 Laravel 应用程序中处理我的编辑表单。
我从表单提交请求所有输入。我得到了:
array:6 [▼
"_method" => "PUT"
"_token" => "iWyonRYFjF15wK8fVXJiTkX09YSPmXukyGbBcHRA"
"phone" => "9786770863"
"email" => "test@sites.com1"
"address" => "23 School St Lowell MA 01851"
"$id" => "1"
]
我的目标是仅验证:电话、电子邮件和地址。
我试过了
$validator = Validator::make(
['phone' => 'max:20'],
['email' => 'required|email|unique:users,email,'. $id ],
['address' => 'max:255']
);
// dd(Input::get('email')); // HERE <------ I got the email to display
if ( $validator->fails()) {
return Redirect::to('user/profile/'. $id )->withErrors($validator)->withInput();
} else {
$user = User::findOrFail($id);
$user->phone = Input::get('phone');
$user->email = Input::get('email');
$user->address = Input::get('address');
$user->save();
它总是对我失败并说
The email field is required.
但如果我没记错的话,电子邮件字段就在那里。
如何在 php Laravel 5 中只验证某些字段?
【问题讨论】:
-
我收到了显示
"test@sites.com1"的电子邮件 -
我认为 id 的串联可能是问题所在。你为什么这样做?
-
同样的错误。这是非常有线的。当我起飞
required时,它起作用了。但这将是一场灾难。 -
我这样做是因为我想在编辑时忽略电子邮件的独特性。
-
你调用函数的方式不对。首先是您要检查的数据,其次是规则。
标签: php validation laravel laravel-5