Thanks to visit codestin.com
Credit goes to github.com

Skip to content

netteForms: min/max wrong validation of numbers #313

@tvape

Description

@tvape

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;
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions