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

Form validation for new pico recipe fails based on values entered #339

Closed
trmong opened this issue Apr 18, 2022 · 1 comment
Closed

Form validation for new pico recipe fails based on values entered #339

trmong opened this issue Apr 18, 2022 · 1 comment
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@trmong
Copy link

trmong commented Apr 18, 2022

In the file app/static/js/pico_recipe.js the validate(form) function fails depending on the values entered. Because of the logic it can fail because some of the min values for "numbers" are set to 0 in the html file. In addition the logic is comparing strings not floating point values. This can cause variable logic results. It can be fixed by using the parseFloat() function to properly convert the values before comparison.

Here is the original code:

 function validate(form) {
    let valid = true;
    for (element of form.getElementsByTagName('input')) {
        const $feedback = $(element).siblings(".invalid-feedback", ".invalid-tooltip");
        if (element.type == "text" && element.pattern) {
            const re = new RegExp(element.pattern)
            if (!re.test(element.value)) {
                $feedback.show();
                valid = false;
            }
        }

        if (element.type == "number" && (element.min || element.max)) {
            if ((element.min && element.value < element.min) || (element.max && element.value > element.max)) {
                $feedback.show();
                valid = false;
            }
        }
    };

    $(form).addClass("was-validated");
    return valid;
 }

Here is my proposed solution that fixes the issues I had....

 function validate(form) {
	let valid = true;
	for(element of form.getElementsByTagName('input')) {
		const $feedback = $(element).siblings(".invalid-feedback", ".invalid-tooltip");
		if ( element.type == "text" && element.pattern ) {
			const re = new RegExp(element.pattern)
			if ( !re.test(element.value) ) {
				$feedback.show();
				valid = false;
				break;
			}
		} else if ( element.type == "number" ) {
			if ( element.min != "" ) {
				if ( parseFloat(element.value) < parseFloat(element.min) ) {
					$feedback.show();
					valid = false;
					break;
				}
			}
			if ( element.max != "" ) {
				if ( parseFloat(element.value) > parseFloat(element.max) ) {
					$feedback.show();
					valid = false;
					break;
				}
			}
		}
	};

	$(form).addClass("was-validated");
	return valid;
}

Thanks

Tab

@chiefwigms chiefwigms added the duplicate This issue or pull request already exists label Apr 21, 2022
@chiefwigms
Copy link
Owner

duplicate to #338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants