laravel 5.4在请求验证之前修改数据

Tho*_*ini 6 php validation laravel laravel-5.4

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

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

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

protected function prepareForValidation()
Run Code Online (Sandbox Code Playgroud)

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

这是我的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');
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我首先尝试覆盖getValidatorInstance方法,因为这看起来像是常见的方法,但它没有被执行(所以没有被覆盖 - 受保护?).

ARI*_*ANA 14

虽然我没有尝试,但它似乎应该工作,你可以validationDataIlluminate\Foundation\Http\FormRequest类重写.

/**
 * Get data to be validated from the request.
 *
 * @return array
 */
protected function validationData()
{
    $all = parent::validationData();
    //e.g you have a field which may be json string or array
    if (is_string($playerIDs = array_get($all, 'player_id')))
        $playerIDs = json_decode($playerIDs, true);

    $all['player_id'] = $playerIDs
    return $all;
}
Run Code Online (Sandbox Code Playgroud)

或者你可以覆盖trait中的all方法Illuminate\Http\Concerns\InteractsWithInput

/**
 * Get all of the input and files for the request.
 *
 * @return array
 */
public function all()
{
    $all = parent::all();
    //then do your operation
    if (is_string($playerIDs = array_get($all, 'player_id')))
        $playerIDs = json_decode($playerIDs, true);

    $all['player_id'] = $playerIDs
    return $all;
}
Run Code Online (Sandbox Code Playgroud)


Tho*_*ini -1

好吧我发现错误出在哪里了。我确实拆分了前端请求和后端请求调用。由于我正在处理后端请求,前端请求没有覆盖任何内容......所以这是我的错,没有错误,抱歉浪费时间,但非常感谢社区!