Laravel - validation of the field formatted with BR currency

Asked

Viewed 973 times

-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?

3 answers

1

The best option in this structure you are sending is to simply replace the characters before validation with str_replace()

$fields = $request->all();
$fields['valorTotal'] = str_replace(['.', ','], ['', '.'], $fields['valorTotal']); // Essa linha remove os '.' e substitui ',' por '.' deixando 999.999,99 foramtado como 999999.99
$validator = Validator::make($fields, [
  'valorTotal' => ['required','numeric'],
]);
if($validator->errors()){
    dd($validator->errors());
}
  • Still, the validation lets pass values with multiple points. Ex:9999......9 or 9.. 9

  • 1

    Are you manipulating the errors in the controller after validation? Because 9999......9 or 9.. 9 do not pass by numeric in the Laravel.

  • I changed the code by setting the check to bankruptcy errors

  • 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.

  • 1

    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.

0

In this case only add one number_format((float)$value, '.', ',');

  • number_format() returns a number formatted version. This function accepts one, two, or four parameters (not three).

0

Solved with a regular expression and Rule:

  public function passes($attribute, $value)
  {
    // verifica se está no formato 9.999.999.999,99 e quantos milhares forem necessários.
    $expressao = "/^([1-9]{1}[\d]{0,2}(\.[\d]{3})*(\,[\d]{0,2})?|[1-9]{1}[\d]{0,}(\,[\d]{0,2})?|0(\,[\d]{0,2})?|(\,[\d]{1,2})?)$/";
    return (preg_match($expressao,$value));
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.