-1
I’m using the jQuery Input Mask to put mask in values.
As much as in the front end the mask avoids being informed of any character other than numbers, dots or the comma, it is necessary to validate in the back end as well. Because you can easily remove the JS from the page and send any character in the value field.
However, the validation of Laravel only understands as number the format with the point as a penny separator.
That is, if the value entered in the mask input is 9,999.99, it will not pass the validation. It would only pass if it was 9999.99
I’m using this validation:
Validator::make($request->all(), [
'valorTotal' => ['required','numeric'],
])->validate();
I tried to replace the 9.999.99 format to 9999.99 using with a function I developed. It works fine.
Then with Validationdata() I replace the value in the request:
public function validationData() {
$formatoUS = formatarValor($this->valorTotal, 'us');
return array_merge(
$this->all(),
[
'valorTotal' => $formatoUS
]
);
}
The value is formatted correctly for 9999.99 and the request is updated for him.
But the validation starts to accept values with two or more points in a row (eg 999999999, 9999....999). That is, the validation sticks.
How to solve this?
Still, the validation lets pass values with multiple points. Ex:9999......9 or 9.. 9
– bur
Are you manipulating the errors in the controller after validation? Because 9999......9 or 9.. 9 do not pass by
numeric
in the Laravel.– Erlon Charles
I changed the code by setting the check to bankruptcy errors
– Erlon Charles
Exactly what I thought. In Laravel several points do not pass. So I thought it was strange, that this validation that you put in, and the one that I’d already done, should work. Does not return the error either this way or using the validate() method at the end. It seems that something happens to change anything in the original request.
– bur
I think I get it. The validation, as you suggested (and mine), removes the points first and only then changes the comma by dot. Thus, 9,999,99 becomes 9999.9 In the case of 9999...99, in passing through str_replace, it already removes the dots and has no comma to treat. Soon, 9999...99 returns simply 999999, passing the validation. I will think about a validation that treats this and let’s see if it works.
– bur