change value of $request before validation in laravel 5.5

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Data Transformation: Changing Request Values Before Validation in Laravel

As developers working with the Laravel ecosystem, we frequently encounter scenarios where raw user input needs to be transformed—sanitized, formatted, or augmented—before it is subjected to validation rules. A common requirement is prefixing names, standardizing formats, or applying business logic directly on the incoming request data.

This post addresses a specific challenge: how to effectively change the value of an input field (like first_name) within a Laravel FormRequest before validation runs, and crucially, how to ensure this modified value is correctly reflected when validation fails using the old() helper.

The Challenge with Direct Manipulation in rules()

You are attempting to modify data inside the rules() method of your FormRequest:

public function rules()
{
    if($this->method() == 'POST') {
         return [
             'first_name' => 'required|max:250', // ... other rules
         ];
}

And then trying to modify the input data:

// Attempted modification inside rules()
$input = $this->all();
$input['first_name'] = 'Mr ' . $request->first_name;
$this->replace($input); 

As you discovered, while this code executes during the request lifecycle, manipulating data within the rules() method does not reliably persist those changes in a way that affects subsequent validation checks or the error reporting mechanism (like Laravel’s built-in old input handling). The rules() method is primarily designed to define what must be validated, not to perform complex data transformation.

The Correct Approach: Pre-Processing Before Validation

The best practice in Laravel for performing complex data manipulation before validation is to handle this logic before the validation process officially begins. This keeps your FormRequest focused purely on defining the rules, and your controller or a dedicated service handles the preparation of the data.

However, if you insist on keeping the transformation within the request layer—which is often useful for context-specific formatting—we need to leverage methods that interact directly with the validated input data structure properly.

Implementing Transformation in the FormRequest

Instead of trying to manipulate $this->all() inside rules(), we should perform the transformation logic right after the request object has been initialized, or use a method that manipulates the input state before validation is finalized.

A robust way to achieve this is by performing the manipulation when the request is first received, ensuring the modified values are available for later checks and error reporting.

Here is how you can correctly modify the data within your UserRequest:

namespace App\Http\Requests\Panel\Users;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true; // Or implement your authorization logic
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // We define the base rules here.
        return [
            'first_name' => 'required|max:250',
            // ... other rules
        ];
    }

    /**
     * Prepare the data for validation (or manipulation).
     * This method is called before validation executes.
     */
    public function prepareForValidation()
    {
        // Access the raw input data from the request object.
        $input = $this->all();

        // Perform the desired transformation here.
        if (isset($input['first_name'])) {
            // Apply the transformation logic.
            $input['first_name'] = 'Mr ' . $input['first_name'];
        }

        // Replace the input data with the modified version.
        $this->replace($input);
    }
}

Why prepareForValidation() Works

By moving the transformation logic into the prepareForValidation() method, you execute this code just before Laravel proceeds to run the validation rules defined in rules().

  1. Data Modification: We access the raw input data ($this->all()), modify it, and use $this->replace($input) to update the request object's internal state with the new values.
  2. Validation Context: Since this modification happens before validation, the subsequent validation rules in rules() operate on the already modified data.
  3. Error Reporting (old()): When validation fails, Laravel correctly stores the current state of the request data (which now includes your prefixed name) in the session, making it accessible via the old('first_name') helper exactly as you desire.

This approach aligns perfectly with the principle of separation of concerns, where FormRequest handles input preparation before validation, and the controller remains clean, which is a core philosophy promoted by Laravel (as detailed on resources like https://laravelcompany.com).

Conclusion

Attempting complex data manipulation directly within the rules() method often leads to unexpected behavior regarding state persistence and error reporting. The correct, developer-friendly solution in Laravel is to utilize lifecycle methods like prepareForValidation(). This allows you to cleanly transform user input into the desired format before validation occurs, ensuring your data is accurate, validated correctly, and easily displayed upon failure using helpers like old(). Always prioritize clear separation of concerns when structuring your request handling logic.