8

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?

6 Answers 6

10

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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');

Comments

3

In your form request use function prepareForValidation()

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

Cheers!

1 Comment

This runs before validation and not after validation as the question requires.
2

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'];

Comments

1

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';

2 Comments

yes i know, but i need to create validation at form request
Ok. Then you can make a dd($request) to see if this new field is present.
1

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!');
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.