Laravel验证不如预期运作。

huangapple go评论67阅读模式
英文:

laravel validation does not function as expected

问题

我正在尝试使用Laravel 10创建一个RESTful API,但在尝试验证输入时,验证不按预期工作,而是在我尝试添加已经存在于数据库中的值时,出现了数据库错误,而不是验证过程提供的适当响应。

以下是相关代码部分的翻译:

  • routes/api.php 文件:
    Route::controller(AuthController::class)->group(function () {
        Route::post('register', 'register');
    });
  • app/Controller/AuthController.php 文件:
    public function register(RegisterRequest $request) {
        /**
         * 用于注册用户的一些代码
         */
        $user = new User;
        $user->mobile_no = request('mobile');
    }
  • app/Requests/RegisterRequest.php 文件:
    public function rules(): array
    {
        return [
            'mobile' => 'required|unique:users,mobile_no|ir_mobile:zero',
        ];
    }

    public function messages() {
        return [
            'mobile.unique' => '这里是自定义消息',
        ];
    }

    public function failedValidation(Validator $validator)
    {
        // 下面的函数存在于 `app/helpers.php` 中,并正常工作
        throwBadRequestError([
            'errors' => $validator->errors(),
            'request' => $_REQUEST
        ]);
    }

请注意,这只是相关代码的翻译,没有其他内容。如果您有其他问题,请随时提出。

英文:

I'm trying to create a RESTful API using Laravel 10,

when trying to validate the inputs, validation does not work as expected and I get database error for the unique field (obviously when I try to add the value which already exists in db) instead of the proper response provided by the validation process.

here's routes/api.php file:

Route::controller(AuthController::class)->group(function () {
    Route::post('register', 'register');
});

here's the app/Controller/AuthController.php file:

public function register(RegisterRequest $request) {
    /**
     * some code to register the user
     */
    $user = new User;
    $user->mobile_no = request('mobile')
}

here's the app/Requests/RegisterRequest.php file:

public function rules(): array
{
    return [
        'mobile'=>'required|unique:users,mobile_no|ir_mobile:zero',
    ];
}

public function messages() {
    return [
        'mobile.unique' => 'a custom msg here',
    ];
}

public function failedValidation(Validator $validator)
{
    // the following function exists in `app/helpers.php` and works currectly
    throwBadRequestError([
        'errors' => $validator->errors(),
        'request' => $_REQUEST
    ]);
}

here's a screenshot of postman:
Laravel验证不如预期运作。

why does not the validation throw an error instead of database?

many thanks in advance.

答案1

得分: 1

From SQL error message the column you are updating is users.mobile while you are testing users.mobile_no in the validation.

英文:

From SQL error message the column you are updating is users.mobile while you are testing users.mobile_no in the validation

huangapple
  • 本文由 发表于 2023年4月6日 23:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定