Aaron
Aaron

Reputation: 4480

Laravel validating an integer, but getting a validation error must be an integer

I am trying to validate a phone number. First I strip the '-' out then I convert the inputed string to an integer. When I try to validate the integer I get an error that the input must be an integer. However, I just converted the string to and integer. Why am I getting this error and how do I fix it?

Here is my code

$request->val = str_replace('-', '', $request->val);
                Log::debug($request->val);
                Log::debug(gettype($request->val)); //outputs string
                $request->val = intval($request->val);
                Log::debug(gettype($request->val));//outputs integer
                $this->validate($request, [
                    'val' => 'Integer|min:10|max:15'//Get an error must be interget
                ]);

Upvotes: 1

Views: 833

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50767

Use ->merge to alter the value of a request attribute, do not attempt to mutate it directly, as the state will not be saved:

$request->merge(['val', intval(str_replace('-', '', $request->val))]);

Upvotes: 1

Related Questions