【问题标题】:Laravel : Validation not working when testing on liveLaravel:实时测试时验证不起作用
【发布时间】:2018-04-25 09:37:32
【问题描述】:

在 API 集成中创建具有验证功能的用户验证功能当我尝试通过一些验证来验证用户并在 postman 中检查它时,它在本地机器上运行良好,但是当我在服务器上上传代码并进行测试时,它不工作.

控制器功能:

public function confirmUser(ForgotPasswordRequest $request)
{
    // ForgotPasswordRequest  for validate
    $user  = User::leftjoin('user_details','users.id', '=', 'user_details.user_id')
                ->where('user_details.phone', '=', $request->post('phone'))
                ->where('users.email', '=', $request->post('email'))
                ->first();
    if(!$user) {
        return response()->json([
                'message' => "The credentials you provided cannot be determined to be authentic.!!",
                'status_code' => 404,
        ]);
    }
    $token = JWTAuth::fromUser($user);
    return response()->json([
        'message'       => "Your account is eligible for change password.!!",
        'token'         => $token,
        'status_code'   => 200
    ]);
}

我的forgotPasswordRequest.php 代码:

<?php

namespace App\Api\V1\Requests;

use Config;
use Dingo\Api\Http\FormRequest;

class ForgotPasswordRequest extends FormRequest
{
    public function rules()
    {
        return Config::get('boilerplate.forgot_password.validation_rules');
    }

    public function authorize()
    {
        return true;
    }
}

在config文件夹boilerplate.php文件中包含forgot_password规则:

  'forgot_password' => [

        'validation_rules' => [
        'email' => 'required|email',
        'phone' => 'required|min:11|numeric'
    ]
],

当email 和phone 为空时,它只返回email 验证而不是phone:

{
"error": {
    "message": "422 Unprocessable Entity",
    "errors": {
        "email": [
            "The email field is required."
        ]
    },
    "status_code": 422
}
} 

我做错了什么?它与cookies有关吗?

【问题讨论】:

  • 错误是什么? “它可以在本地工作,但不能在生产环境中”通常是由于未推送其他更改,或者环境完全不同。
  • @Loek 添加了validation 响应。

标签: laravel laravel-5.5 laravel-validation


【解决方案1】:

这里没有任何问题。

Laravel 会检查email 字段是否填写正确。没有填写,Laravel 会跳出验证方法并返回异常。

如果您填写了email 字段,但将phone 字段留空,则验证响应将与phone 字段有关。

【讨论】:

  • 不明白,但是当email字段不为空且phone为NULL时,它直接检查if(!$user)并返回The credentials you provided cannot be determined to be authentic.!!
  • 好吧,这完全是一个不同的问题。也许配置文件缓存在您的实时情况下?尝试运行 php artisan config:clear 和 php artisan config:cache(记住每次推送到生产环境时都要这样做,但前提是你希望缓存文件!)
猜你喜欢
  • 2018-11-09
  • 2017-06-21
  • 2018-01-18
  • 2020-07-24
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 2021-06-15
  • 2021-08-29
相关资源
最近更新 更多