vlucas/valitron

'integer' sometimes give an error?

omexlu opened this issue · 6 comments

Hello,

I use the following code to protect an OTP field (6 numbers).

// google authenticator
declare(strict_types=1);
[...]
$v->rule('required', 'otp')->label(setLabel('otp'));
$v->rule('integer', 'otp')->label(setLabel('otp'));
$v->rule('lengthBetween', 'otp', 6, 6)->label(setLabel('otp'));

However, 'integer' sometimes gives me an error that it must be numbers although only numbers are entered.

Usually this works but sometimes I still get an error message.
What is the reason for this?

EDIT: Maybe related to this: #300 same behavour.
Maybe 'declare(strict_types=1);' makes problems too?

EDIT2: I have now changed the affected row to '$v->rule('integer', 'otp', true)->label(setLabel('otp'));' (attached true), will this resolve the issue in strict_type 1? After some tests it works.

Thanks in advance

Unfortunately the small change with 'true' as an addition still doesn't work as intended :/

Does anyone know what to do?

Can you provide some sample input that causes failures?

It should not be related to #300 : hitting PHP_INT_MAX with a 6 digit number seems unlikely.
I also don't think it has to do with strict_types: setting the parameter to 'true' should deal with that. If the parameter is set to true, the validation will run a preg_match to make sure the input only consists of digits (with a possible - at the front)

FORM:
<div class="form-group"> <label for="otp">OTP</label> <input type="text" autocomplete="off" maxlength="6" name="otp" id="otp" class="otpPassword"> <span class="error"></span> </div>

PHP:

// validate form data
      $v = new Valitron\Validator($_POST);
      $v->rule('required', 'otp')->label(setLabel('otp'));
      $v->rule('integer', 'otp', true)->label(setLabel('otp'));
      $v->rule('lengthBetween', 'otp', 6, 6)->label(setLabel('otp'));

Sometimes it works but many times i get the error that the input is not a interger.

The input is a integer in format '123456' for OTP (6 numbers).
I don't know why this happens.

What I meant was input the validator received. Like what number gives an error (always or sometimes)

The input numbers are always different but are integer, i really don't understand why it give the error that it aren't integer.

Edit: sometimes it passed sometimes it gives an error but only at this script, I never had a issue otherwhere.

EDIT: i remove the integer validation until i found out why it happends, until then i use the regex:
$v->rule('regex', 'otp', '/^[0-9]{6,6}$/');

The input is a ONE TIME PASSWORD :)

So now we have a new problem :) It fails for some input, but we can't know if that input should fail or not, because we don't know what it is.
Moving to regex is a good idea, let's see if this solves your problem. I'm gonna close this for now