marymvlg28's avatar

Modify request input value before validation

Hi,

I have a form with a phone input.

This input has an input mask like this 9999-999 9999

I'm using form request validation (App\Http\Requests\UserRequest.php)

I would like to validate the phone number should have 11 digits (digits:11)

I have tried $this->replace(['phone_number' => str_replace(...)]) and $this->merge(['phone_number' => str_replace(...)])

  • I know $this->replace will replace all request inputs

But none of those seems to work. Validation rule still take the original input.

can anybody help me?

0 likes
17 replies
bobbybouwmann's avatar
Level 88

This might help: http://stackoverflow.com/questions/31850622/modify-input-before-validation-on-laravel-5-1

You need to override the getValidatorInstance function and do the changes there, here is an example

public function rules()
{
    return [
        'email' => 'required|email',
        'phone_number' => 'numeric',
    ];
}

public function getValidatorInstance()
{
    $this->formatPhoneNumber();
    
    parent::getValidatorInstance();
}

protected function formatPhoneNumber()
{
    $this->request->merge([
        'phone_number' => str_replace('-', '', $this->request->get('phone_number'));
    ]);
}

Something like this should do!

4 likes
marymvlg28's avatar

Thank u! It did work!

    public function getValidatorInstance()
    {
        $this->cleanPhoneNumber();
        return parent::getValidatorInstance();
    }

    protected function cleanPhoneNumber()
    {
        if($this->request->has('phone_number')){
            $this->merge([
                'phone_number' => str_replace(['-','_',''], '', $this->request->get('phone_number'))
            ]);
        }
    }
3 likes
Azirius's avatar

I have been trying to do something like this, but for some reason it was not working for me. Currently working with Laravel 5.6.*

    protected function overrideWithUsersDetails()
    {
        if (auth()->check()) {
            $this->merge(array_replace_recursive($this->all(), ['message' => [
                'email' =>  auth()->user()->email,
                'name' =>  auth()->user()->name,
            ]]));
        }
    }

    public function getValidatorInstance()
    {
        $this->overrideWithUsersDetails();

        return parent::getValidatorInstance();
    }

This for me was the only way to replace the users submitted data, with the data I wanted to send along. Looks similar to the above approach, but did not quite work for me in its current form. Hope this helps someone out! Did not want to necro a thread, but might be a valid addition.

1 like
rehmat's avatar

@BOBBYBOUWMANN - Should return parent::getValidatorInstance(); from getValidatorInstance() method. Currently, it returns null.

Reverseternity's avatar

@Elenktik Man, you are genius! All this time the best solution was right in documentation 😃 Thanks!

1 like
Dumilson's avatar

@starter-dev testei e não esta funcionando a modificação. meu codigo.

´´´ public function prepareForValidation(): void { foreach ($this->allFiles() as $field => $file) { if ($this->hasFile($field)) { $path = $this->uploadFile($field); $this->tempFiles[$field] = $path; $this->merge([$field => $path]); } } } ´´´

martinbean's avatar

@Dumilson Please create your own thread if you’re having issues, instead of bumping one that was initially create almost a decade ago.

Please or to participate in this conversation.