php - Allow only numeric and float values with validation in Laravel
Get the solution ↓↓↓I have a field{-code-1}
in my model, now I want to only save{-code-2}
orfloat
values in this column.
Values can be like:
1
2
0.5
2.5
I tried to usenumeric
but then it is not allowing me to inputfloat
values:
$this->validate($request, [
'{-code-1}' => 'required|numeric|min:1|max:50',
]);
Any help is highly appreciated.
Thanks
Answer
Solution:
You can use closures if no validation rule helps you.
So, first try this:
'days' => 'required|numeric|min:0|max:50',
If it is not working, we can use closures:
'days' => [
'required',
'numeric',
function ($attribute, $value, $fail) {
if ($value <= 0) {
$fail($attribute.' must be greater than 0.');
}
},
'max:50',
],
Answer
Solution:
You can try custom validation rules for your case. Example:
'days' => 'required|regex:/^\d*(\.\d{2})?$/'
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object not found by the @paramconverter annotation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.