laravel 5.4在请求中验证之前修改数据 [英] laravel 5.4 modify data before validation in request

查看:313
本文介绍了laravel 5.4在请求中验证之前修改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的自定义请求,该请求扩展了Backpack CrudController.

I have my custom Request, which extends the Backpack CrudController.

现在,我想覆盖ValidatesWhenResolvedTrait的prepareForValidation,因为它看起来像是修改传入数据的正确位置,但我不知道如何...

Now I would like to override the prepareForValidation of the ValidatesWhenResolvedTrait since it looks like the right place to modify my incoming data, but I can't figure out how ...

所以我的第一个问题是,我可以覆盖此方法吗?受保护的...

So my first question is, can I override this method? Its protected ...

protected function prepareForValidation()

第二个问题,如何修改Request或FormRreuqest对象上的输入?

这是我的RequestClass

Here is my RequestClass

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Config;

class DonationsRequest extends \Backpack\CRUD\app\Http\Requests\CrudRequest
{


    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // only allow updates if the user is logged in
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|max:255',
            'email' => 'required|email',
            'dob' => 'required|date',
            'newsletter' => 'required|boolean',
            'country' => 'sometimes|required|in:'.implode(',', Config::get('validation.countries')),
            'street' => 'sometimes|required|string|max:255',
            'zip' => 'sometimes|required|string|between:4,5',
            'city' => 'sometimes|required|string|between:4,255',
            'amount' => 'required|numeric|between:1,'.Config::get('donations.max'),
            'type' => 'required|in:oo,monthly',
            'provider' => 'sometimes|string|nullable',
            'product_id' => 'sometimes|exists:products,id|nullable',
            'campaign_id' => 'required|exists:campaigns,id',
            'status' => 'sometimes|required|in:pending,canceled,success,error',
            'profile' => 'sometimes|string|regex:/^profile[0-9]+$/|nullable',
        ];
    }

    /**
     * Get the validation attributes that apply to the request.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            //
        ];
    }

    /**
     * Get the validation messages that apply to the request.
     *
     * @return array
     */
    public function messages()
    {
        return [
            //
        ];
    }

    private function prepareForValidation()
    {

        dd('getValidatorInstance custom');

        $this->sanitizeInput();

        return parent::getValidatorInstance();
    }

    private function sanitizeInput()
    {

        dd('sanitizeInput custom');

        $data = $this->all();

        dd($data);

        // overwrite the newsletter field value to match boolean validation
        $data['newsletter'] = ($data['newsletter'] == 'true' || $data['newsletter'] == '1' || $data['newsletter'] == true) ? true : false;

        return $data;
    }

    private function validate() {
        dd('validate');
    }
}

如您所见,我首先尝试重写getValidatorInstance方法,因为这看起来像是对此的普通做法,但并未执行(因此不被覆盖-是否受保护?).

As you can see, I first tried to override the getValidatorInstance method, since this looked like the common aproach to this, but it is not executed (so not overridden - protected?).

推荐答案

好,我发现了错误所在.我确实拆分了前端请求和后端请求调用.由于我正在处理后端请求,因此前端请求没有覆盖任何内容……这是我的错,那里没有错误,为浪费时间感到抱歉,但是对社区表示感谢!

Ok I found out where the error was. I did split the Frontend Request and the Backend Request Call. Since I was working on the Backend Request the Frontend Request was not overwriting anything ... so it was my bad, no bug there, sry for the waste of time, but a big thanks to the community!

这篇关于laravel 5.4在请求中验证之前修改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆