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

heizenberg's avatar

How to convert my input to integer

I'm having problem validating Laravel 5.1 Form Request. I have Input number and I am trying to validate it if it is a digit.

My FormRequest

public function rules()
{
    return [
        'width' => 'required|digits_between:1,5'
    ];
}

dd Result:

array:2 [
    "_token" => "Md7Q8s6iAUw8JytippV5dPV5FIgKX7OeVKbhLpZm"
    "width" => "20"
]

I check the result but the width return a string. How can I convert it to integer. I see that JeffreyWay did that in a tutorial and I forget what tutorial it is.

The result should be:

array:2 [
    "_token" => "Md7Q8s6iAUw8JytippV5dPV5FIgKX7OeVKbhLpZm"
    "width" => 20
]
0 likes
20 replies
mstnorris's avatar

From the form, it will be a string unless you're using a select list where you can specify the type in the value field.

http://html5doctor.com/html5-forms-input-types/ may help you with client side validation although not 100%. You'll have to cast the value to an integer using php: see http://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php.


As for when the data is spit back out, check out attribute casting - https://mattstauffer.co/blog/laravel-5.0-eloquent-attribute-casting.

In your model, add:

protected $casts = [
    'width' => 'integer',
];
1 like
heizenberg's avatar

I am using LaravelCollection and this is my form input:

{!! Form::number('width',  1, ['min' => 1, 'max' => 5, 'class' => 'form-control']) !!}

I am using FormRequest So I think that is not what I'm looking for? My FormRequest will validate the form input and the form input is string instead of a digits

mstnorris's avatar

That's what I said, you will have to cast the the value to an integer in the form request before you try and validate it. As far as I know, the Laravel Collective Number form input, just uses the HTML 5 number input, which just ensures the user enters only numbers.

BUT, the value is still a string, which you need to convert to a number (integer) which is where the (int) casting comes from that I showed you above.

mstnorris's avatar

@heizenberg that isn't what I said, I said attribute casting is for when you are getting the data back out.

Did you check out the Stack Overflow question I posted?

$num = "3.14"; // input from form field
$int = (int)$num; // integer version of the form input
heizenberg's avatar

@mstnorris Yup.

That's not what I am looking for I think. What I need is when user input to a field with an integer the result is actually a string.

array:1 [
    "width" => "20"
]

Ofcourse that FormRequest will fail. I can't convert it to my controller store method because the FormRequest will automatically fire when my input is invalid.

mstnorris's avatar

That isn't what you asked originally. You asked to convert a string to an integer.

In your form request, cast the string version of the input to an integer.

heizenberg's avatar

@mstnorris That isn't what you asked originally. You asked to convert a string to an integer.

In your form request, cast the string version of the input to an integer.?

How? Please give an example

Snapey's avatar

digits_between:min,max is between 1 and 5 digits, ie any number between 0 and 99999

try 'width' => 'required|digits|between:1,5',

Then in the controller you can $width = int($request->width);

2 likes
Snapey's avatar

My earlier example does not work.

This is better; 'width' => 'required|max:5', but it looks like a custom rule is in order since you can also enter '1.5'

how about 'required|in:1,2,3,4,5'

skliche's avatar
skliche
Best Answer
Level 42

@heizenberg If you want to accept integers between 1 and 5 only, try the following validation rule:

'width' => 'required|integer|between:1,5'

If the validation passes you can convert to an integer like this:

(int)$request->get('width')
3 likes
andreasb's avatar

May I push this again?

In my view the suggested solution does not make sense - since the StoreBlogPostRequest where validation is done will be called BEFORE the value is casted in the controller.

So in the Request it will not be cast so the validation will fail.

What is the solution to this? Or in other words: how can I cast fields in my request array before sending them to the Request to validate which then goes to the controller?

Thanks Andreas

1 like
kt200707g11's avatar

user this package to validate and cast input in one line code

composer require qtlenh/laravel-strict-validator

this package add new parameters strict and cast to the existing Laravel rules: integer, numeric, decimal, boolean.

example validation rule:

'width' => 'integer:cast'

kt200707g11's avatar

@tisuchi I provide a solution for others to refer to if they encounter a similar issue and are seeking a solution. As of now (Laravel 11), there is still no support for checking input data types and casting input data types.

rimisen49's avatar

To convert an input to an integer in many programming languages, you can use a function or method provided by the language. Here's how you might do it in Python:

python Copy code

Get input from the user

user_input = input("Enter a number: ")

Convert input to an integer

try: integer_input = int(user_input) print("Integer value:", integer_input) except ValueError: print("Invalid input. Please enter a valid integer.") In this example, input() is used to get input from the user, which is stored as a string in the variable user_input. The int() function is then used to convert this string to an integer. If the conversion is successful, the integer value is printed. If the user enters something that can't be converted to an integer (like a word or a symbol), a ValueError is raised, and the program prints an error message.

Please or to participate in this conversation.