Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'integer' sometimes give an error? #313

Closed
omexlu opened this issue May 13, 2020 · 6 comments
Closed

'integer' sometimes give an error? #313

omexlu opened this issue May 13, 2020 · 6 comments
Labels

Comments

@omexlu
Copy link

omexlu commented May 13, 2020

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

@omexlu
Copy link
Author

omexlu commented May 14, 2020

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

Does anyone know what to do?

@willemwollebrants
Copy link
Collaborator

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)

@omexlu
Copy link
Author

omexlu commented May 14, 2020

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.

@willemwollebrants
Copy link
Collaborator

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

@omexlu
Copy link
Author

omexlu commented May 14, 2020

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 :)

@willemwollebrants
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants