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.
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
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.
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.
In the example there is a Controller with method store:
public function store(StoreBlogPostRequest $request)
{
// The incoming request is valid...
// Do your casting of data
$width = (int) $request->get('width');
}
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?
@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.
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.