Laravel 5 Form Request data pre-manipulation
in laravel 5.1 you can do that
<?php namespace App\Http\Requests;class UpdateSettingsRequest extends Request {public function authorize(){ return true;}public function rules(){ return [];}protected function getValidatorInstance(){ $data = $this->all(); $data['date_of_birth'] = 'test'; $this->getInputSource()->replace($data); /*modify data before send to validator*/ return parent::getValidatorInstance();}
Since Laravel 5.4, you can use the prepareForValidation
method on FormRequest classes.
<?phpnamespace App\Http\Requests;use Illuminate\Foundation\Http\FormRequest;class StorePostRequest extends FormRequest{ /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => 'required|max:200', 'body' => 'required', 'tags' => 'required|array|max:10', 'is_published' => 'required|boolean', 'author_name' => 'required', ]; } /** * Prepare the data for validation. * * @return void */ protected function prepareForValidation() { $this->merge([ 'title' => fix_typos($this->title), 'body' => filter_malicious_content($this->body), 'tags' => convert_comma_separated_values_to_array($this->tags), 'is_published' => (bool) $this->is_published, ]); }}
There's a more detailed write-up here:https://sampo.co.uk/blog/manipulating-request-data-before-performing-validation-in-laravel
Note: This question and answer were both posted before Laravel 5.1 was released and both target 5.0. For 5.1 and above, see this answer by @Julia Shestakova or this answer by @BenSampo for a more modern solution.
After some messing around myself I've come up with the following:
app/Http/Requests/Request.php
<?php namespace App\Http\Requests;use Illuminate\Foundation\Http\FormRequest;abstract class Request extends FormRequest { /** * Override the initialize method called from the constructor to give subclasses * an opportunity to modify the input before anything happens. * * @param array $query * @param array $request * @param array $attributes * @param array $cookies * @param array $files * @param array $server * @param null $content */ public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) { parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content); // Grab the input $data = $this->getInputSource()->all(); // Pass it off to modifyInput function $data = $this->modifyInput($data); // Replace modified data back into input. $this->getInputSource()->replace($data); } /** * Function that can be overridden to manipulate the input data before anything * happens with it. * * @param array $data The original data. * @return array The new modified data. */ public function modifyInput(array $data) { return $data; }}
Then on extending classes you can just override the modifyInput
method like this:
app/Http/Requests/TestRequest.php
<?php namespace App\Http\Requests;class TestRequest extends Request { public function authorize() { return true; } public function rules() { return []; } /** * Modify the input. */ public function modifyInput(array $data) { $data['date_of_birth'] = 'something'; // Make sure to return it. return $data; }}
This seems to work for my needs. I'm not sure of any drawbacks of doing it this way so I welcome any comments/criticism.
The answer given by The Shift Exchange above will also work just fine.