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

netteForms: min/max wrong validation of numbers #313

Closed
tvape opened this issue Jan 11, 2024 · 1 comment
Closed

netteForms: min/max wrong validation of numbers #313

tvape opened this issue Jan 11, 2024 · 1 comment

Comments

@tvape
Copy link

tvape commented Jan 11, 2024

Version: 3.1.14

Bug Description

Bug is related to the commit netteForms: min/max/range can compare strings.

When a form control is used as an argument in the rule, then a numeric string is passed to the js validator and it's not parsed as float, so there is a wrong comparison made.

Example: '100' >= '2' is false in JavaScript.

Steps To Reproduce

$form = new \Nette\Forms\Form();

$form->addFloat('min_age', 'Min age')
    ->setDefaultValue(6);

$form->addFloat('max_age', 'Max age')
    ->setDefaultValue(100)
    ->addRule(\Nette\Forms\Form::Min, null, $form['min_age']);

$form->addSubmit('save', 'Save');

echo $form;

// on submit js error: Please enter a value greater than or equal to 6.

Expected Behavior

Floats and integers should be compared as numbers, not as strings.

Possible Solution

min: function(elem, arg, val) {
	if ((/^-?[0-9]*\.?[0-9]+$/).test(arg)) {
		val = parseFloat(val);
	}
	return val >= arg;
},

max: function(elem, arg, val) {
	if ((/^-?[0-9]*\.?[0-9]+$/).test(arg)) {
		val = parseFloat(val);
	}
	return val <= arg;
},
@tvape tvape changed the title netteForms: min/max wrong validation netteForms: min/max wrong validation of numbers Jan 12, 2024
@sasule
Copy link

sasule commented Feb 1, 2024

Confirming that bug.

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

No branches or pull requests

2 participants