gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:07:46 25 4
gpt4 key购买 nike

<分区>

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

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

所以我的第一个问题是,我可以重写这个方法吗?它 protected ...

protected function prepareForValidation()

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

这是我的请求类

<?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 方法,因为这看起来像是对此的常见方法,但它没有被执行(所以没有被覆盖 - protected ?)。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com