Laravel Form Request Add Custom Variable After Validation

This is my form request code, i want to add new variable after validation success, so i can access that variable at my controller :

class CouponRequest extends Request
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'start_year' => 'required',
            'start_month' => 'required',
            'start_day' => 'required',
            'start_time' => 'required',
            'finish_year' => 'required',
            'finish_month' => 'required',
            'finish_day' => 'required',
            'finish_time' => 'required',
        ];
    }

    public function afterValidation()
    {
        $this->start_date = Carbon::create( $this->start_year, $this->start_month, $this->start_day );
    }
}

So after validation has no error, i can call this instance at my controller :

$request->start_date;

Could i do this?

Upvotes: 8

Views: 16479

Answers (6)

free2idol1
free2idol1

Reputation: 320

In laravel 8 and above, you can do this:

Option 1:

public function safe(array $keys = null): \Illuminate\Support\ValidatedInput|array
{
   $validated_input =  parent::safe($keys);

   return $validated_input->merge([
       'foo' => "bar",
   ]);
 }

Then in controller, you can access foo by:

$request->safe()->foo;

Option 2:

public function validated($key = null, $default = null)
{
    $validated = parent::validated($key = null, $default = null);

    return array_merge($validated, [
        'foo' => 'bar'
    ]);
}

Then in controller:

$request->validated()['foo'];

Upvotes: 1

ChinwalPrasad
ChinwalPrasad

Reputation: 327

All above methods work but in my opinion I would override the passedValidation method in the form request class. This method is called after the validation checks are passed and hence keep the data clean.

Ex.

public function passedValidation()
{
    $this->merge([
       'start_date' => Carbon::create( $this->start_year, $this->start_month, $this->start_day )
    ]);
}

If you dump the data now you should see your new start_date value as well.

Upvotes: 10

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

In your form request use function prepareForValidation()

protected function prepareForValidation(): void
{
    $this->merge([
        'start_date' => Carbon::now()
    ]);
}

Cheers!

Upvotes: 3

ahmeti
ahmeti

Reputation: 504

I am using this method after success request for manipulations.

Source: 50116187/1101038

public function withValidator(Validator $validator)
{
    if ( $validator->fails() ) {

        \Log::info('Error! No Manipulation!');

    }else{

        $this->merge([
            'custom' => 'Test Manipulation!'
        ]);

        \Log::info('Success Manipulation!');
    }

}

Upvotes: 1

Jari Pekkala
Jari Pekkala

Reputation: 887

You could do this

public function afterValidation()
{
    $this->request->add([
        'start_date' => Carbon::create($this->start_year, $this->start_month, $this->start_day)
    ]);
}

public function validate()
{
    parent::validate();

    $this->afterValidation();
}

And then access the attribute in your controller as

$request->get('start_date');

Upvotes: 4

Jesús Amieiro
Jesús Amieiro

Reputation: 2483

I made the validation in the Controller. The method has a "Request $request" parameter. I have a I do this:

$input = $request->all();
$input['my_new_field] = 'the_data';

Upvotes: 1

Related Questions