prepareForValidation in livewire

What seems to be the problem: I am hoping to use the prepareForValidation() method within a livewire component.

Steps to Reproduce: I want to modify some of the request data before validation, in a standard laravel FormRequest i use the prepareForValidation() method.
In livewire I have tried this, livewire throws an error and says i must follow the prepareForValidation($attributes), which I have applied. If I put a dd($attributes) I can see the contents of the request, however at this point I am not sure how to go about modifying that data, in the laravel form request I would simply have $this->merge([]); within the prepareForValidation() method.
In this case $attributes is an array not a collection. so merge() isn’t useable.

I have tried simply modifying and returning the attribute i require modifying but this doesnt work as it throws errors on the other attributes that haven’t been modified.

I am just curious as to whether there is a standard way of doing this in a livewire component as i can’t see anything in the documents.

Are you using the latest version of Livewire: Yes

Do you have any screenshots or code examples: Yes

Hey there. As long as I know, FormRequest is no longer functional in Livewire, not in the way as you could do it in Laravel. Once you define the validation for the property in Livewire, before call the validate method you could call a self method that change the property will be evaluated.

Thanks @prospero second time you’ve helped me out.
I dug into the code of the validation file and eventually got it working by using the following functionality

I hope that somebody finds this useful in the future.

1 Like