【问题标题】:Laravel/Lumen email trimming for api用于 api 的 Laravel/Lumen 电子邮件修剪
【发布时间】:2018-05-11 12:15:18
【问题描述】:

我正在使用 Laravel/Lumen 开发一个 API。我看到很少有用户抱怨即使他们的电子邮件完全没问题,我的 api 回复说The email must be a valid email address.

我看到的是,他们在'noob@user.com ' 之类的电子邮件之后错误地给了一个空格。结果,系统不接受电子邮件。到目前为止,我在代码中使用的是:

try {
       $this->validate($request, [
        'first_name' => 'required|min:3|max:40',
        'last_name'  => 'required|min:3|max:40',
        'email'      => 'required|email|unique:clients,email',
        'profile_photo' => ''
         ]);
        } catch (ValidationException $e) {
            return response()->json($this->clientTransformer->validationFailed($e), 200);
     }

我尝试在 try 块的第一行中添加以下行,但未能更改 $request 对象属性。

  try{
    $request->email = trim($request->email, ' '); //<= or
    $request->email = str_replace(' ', '', $request->email); // <= this line

$this->validate($request, [
                'first_name' => 'required|min:3|max:40',
                'last_name'  => 'required|min:3|max:40',
                'email'      => 'required|email|unique:clients,email',
                'profile_photo' => ''
                 ]);
    }

但这些都不起作用。这是将完全相同的电子邮件传递给 validate 方法。有什么快速的方法吗?

【问题讨论】:

  • 您可以在发送请求之前使用javascript的trim方法使其工作。

标签: php laravel validation lumen


【解决方案1】:

你可以使用:

$request->replace(array('email' => trim($request->email)));

$request->merge(array('email' => trim($request->email)));

来源:

https://laracasts.com/discuss/channels/general-discussion/laravel-5-modify-input-before-validation

【讨论】:

  • $request->merge() 完美运行,$request->replace() 仅用一个数组项(电子邮件)替换所有数组。这不是预期的......无论如何......谢谢..回答已接受
猜你喜欢
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 2023-02-09
  • 2018-10-19
  • 2016-12-18
  • 1970-01-01
相关资源
最近更新 更多