Php Laravel 5.1在表单请求验证之前修改输入
在进行验证之前,是否有方法修改表单请求类中的输入字段 我想修改一些输入日期字段,如下所示,但它似乎不起作用 当我将Php Laravel 5.1在表单请求验证之前修改输入,php,validation,laravel-5,laravel-5.1,Php,Validation,Laravel 5,Laravel 5.1,在进行验证之前,是否有方法修改表单请求类中的输入字段 我想修改一些输入日期字段,如下所示,但它似乎不起作用 当我将$this->start_dt输入字段设置为2016-02-06 12:00:00和$this->end_dt设置为2016-02-06 13:00:00时,我仍然会收到验证错误“end_dt必须在start_dt之后”。这意味着在rules()函数内更新$this->start_dt和$this->end_dt时,输入请求值不会更改 公共功能规则() { 如果($this->sta
$this->start_dt
输入字段设置为2016-02-06 12:00:00
和$this->end_dt
设置为2016-02-06 13:00:00
时,我仍然会收到验证错误“end_dt必须在start_dt之后”。这意味着在rules()
函数内更新$this->start_dt
和$this->end_dt
时,输入请求值不会更改
公共功能规则()
{
如果($this->start\u dt){
$this->start_dt=Carbon::createFromFormat('d M Y H:i:s',$this->start_dt.'.$this->start_hr.:'.$this->start_min.:00');
}
如果($this->end_dt){
$this->end_dt=Carbon::createFromFormat('d M Y H:i:s',$this->end_dt.'.$this->end_hr.:'.$this->end_min.:00');
}
返回[
“开始日期”=>“必需日期”在:昨天之后,
'end_dt'=>'所需日期|在:start_dt | before:'之后。Carbon::parse($this->start_dt)->addDays(30)
];
}
注意:
start\u dt
和end\u dt
是日期选择器字段,start\u hr
,start\u min
是下拉字段。因此,我需要通过组合所有字段来创建datetime,以便进行比较 您可以执行以下操作:
public function rules(Request $request)
{
if ($request->has('start_dt')){
$request->replace('start_dt', Carbon::createFromFormat('d M Y H:i:s', $request->start_dt . ' ' . $request->start_hr . ':'. $request->start_min . ':00'));
}
if ($request->has('end_dt')){
$request->replace('end_dt' ,Carbon::createFromFormat('d M Y H:i:s', $request->end_dt . ' ' . $request->end_hr . ':'. $request->end_min . ':00'));
}
return [
'start_dt' => 'required|date|after:yesterday',
'end_dt' => 'required|date|after:start_dt|before:' . Carbon::parse($request->start_dt)->addDays(30)
];
}
从laravel 5.4开始,您可以覆盖
ValidatesWhenResolvedTrait
的prepareForValidation
方法来修改任何输入。对于laravel 5.1,也应该有类似的功能
请求中的示例
/**
* Modify the input values
*
* @return void
*/
protected function prepareForValidation() {
// get the input
$input = array_map('trim', $this->all());
// check newsletter
if (!isset($input['newsletter'])) {
$input['newsletter'] = false;
}
// replace old input with new input
$this->replace($input);
}
FormRequest有一个方法validationData(),该方法返回要验证的数据,因此我将在我的表单请求中覆盖它:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MyClassRequest extends FormRequest
{
...
/**
* Get data to be validated from the request.
*
* @return array
*/
public function validationData() {
return array_merge(
$this->all(),
[
'number' => preg_replace("/[^0-9]/", "", $this->number)
]
);
}
...
}
尝试以下步骤:
1-中间件
首先,您应该在app/Http/middleware
中创建一个中间件:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TransformsRequest;
class DateTimeMiddleware extends TransformsRequest
{
protected $fields = [
'birth_date' => 'toGregorian',
'created_at' => 'toDateTime',
];
protected function transform($key, $value)
{
if (!array_key_exists($key, $this->fields)) {
return $value;
}
$function = $this->fields[$key];
return call_user_func($function, $value);
}
}
我对此采取了另一种方法,因为我希望能够使用$model->fill($validated)代码>在我的控制器中。因此,我需要确保复选框已包含为false
,否则将从数组中排除
因此,我在app\Traits\ConvertBoolean.php中创建了一个trait,如下所示:
<?php
namespace App\Traits;
trait ConvertBoolean
{
// protected $boolean_attributes = [];
/**
* Override the FormRequest prepareForValidation() method to
* add boolean attributes specified to the request data, setting
* their value to the presence of the data in the original request.
*
* @return void
*/
protected function prepareForValidation() {
if (isset($this->boolean_attributes) && is_array($this->boolean_attributes)) {
$attrs_to_add = [];
foreach ($this->boolean_attributes as $attribute) {
$attrs_to_add[$attribute] = $this->has($attribute);
}
$this->merge($attrs_to_add);
}
}
}
use App\Traits\ConvertBoolean;
class MyUpdateRequest extends FormRequest
{
use ConvertBoolean {
prepareForValidation as traitPrepareForValidation;
}
protected function prepareForValidation() {
// the stuff you want to do in MyRequest
// ...
$this->traitPrepareForValidation();
}
// ...
}
如果您想在请求中使用$this->prepareForValidation()
,这仍然是可能的
更改MyRequest,如下所示:
<?php
namespace App\Traits;
trait ConvertBoolean
{
// protected $boolean_attributes = [];
/**
* Override the FormRequest prepareForValidation() method to
* add boolean attributes specified to the request data, setting
* their value to the presence of the data in the original request.
*
* @return void
*/
protected function prepareForValidation() {
if (isset($this->boolean_attributes) && is_array($this->boolean_attributes)) {
$attrs_to_add = [];
foreach ($this->boolean_attributes as $attribute) {
$attrs_to_add[$attribute] = $this->has($attribute);
}
$this->merge($attrs_to_add);
}
}
}
use App\Traits\ConvertBoolean;
class MyUpdateRequest extends FormRequest
{
use ConvertBoolean {
prepareForValidation as traitPrepareForValidation;
}
protected function prepareForValidation() {
// the stuff you want to do in MyRequest
// ...
$this->traitPrepareForValidation();
}
// ...
}
嗨,Fabulusco,你能告诉我你的FormRequest和你在哪里使用它的控制器方法吗?嗨,Yassine,Thomas工作的例子!无论如何,谢谢:)如果你把这一行$This->replace($data)放在下面,这对我很有用:$This->replace($data=array\u merge($This->all(),['number'=>preg\u replace(“/[^0-9]/”,“,$This->number)];$This->replace($data);return$data;}这有文件记录吗?否则,它可能会像以前的其他解决方案一样,在小版本升级中悄无声息地崩溃。另请参阅: