Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ehsanm's avatar

prepareForValidation method not working

Hi I want my slug input sanitized before applying validation rules but when I use prepareForValidation method and die & dump my form request data, I have non-sanitized data again! What I missing here?

Here is what I tried in my controller :

/**
 * Prepare the data for validation.
 *
 * @return void
 */
protected function prepareForValidation()
{
    $this->merge([
        'slug' => Str::slug($this->slug),
    ]);
}
0 likes
6 replies
bobbybouwmann's avatar

This method should do exactly what you need. Where are you dumping the code?

ehsanm's avatar

At store or update method before writing any code.

Snapey's avatar

You will only see the effect of this AFTER validation. Is that where you are looking?

Snapey's avatar

Your function needs to live within a FormRequest class, but you mention your controller

ehsanm's avatar

No, I want to apply Str::slug($this->slug) to request's slug input and then in validation check unique:table validation rule.

Snapey's avatar
Snapey
Best Answer
Level 122

So, whats your point? Did you understand my reply?

You can only use this function if you use a FormRequest class

Please or to participate in this conversation.