1

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

1 Answer 1

1

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))]);
Sign up to request clarification or add additional context in comments.

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.