laravel 5.4 modify data before validation in request [closed]

你。 提交于 2019-12-05 08:04:04

Although I didn't tried but it seems it should work you can override validationData from Illuminate\Foundation\Http\FormRequest class like.

/**
 * 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;
}

or you can override all method in Illuminate\Http\Concerns\InteractsWithInput trait

/**
 * 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;
}

Could you modify the request?

$request->merge(['field' => 'new value']);
RajG

Well I am sure,this can help in modifying The input, it worked for me.[laravel 5.4]

place this

$input['url'] = $url;
$this->replace($input);
dd($input);

in listFormRequest. (use $all instead of $input, if you follow above used answer).

This only changes input,which is available even in controller. You still need to find a way to insert it into DB, or do something else to use modified input for using it in blade.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!