formapro / JsFormValidatorBundle

The Javascript validation for Symfony 2, 3 and 4 forms
MIT License
128 stars 57 forks source link

Url constraint types "http://" with every keystroke #165

Open benr77 opened 2 years ago

benr77 commented 2 years ago

There is a fault with Url constraint. Every time I type a character into a URL field, it prepends http:// so I end up with http://http://http://http://http://http://http://thestartofmyurl etc.

It looks like it is simply because there is no check to see if it's already prefixed http://.

66Ton99 commented 2 years ago

Need more details!

benr77 commented 2 years ago

In this constraint: https://github.com/formapro/JsFormValidatorBundle/blob/master/src/Resources/public/js/constraints/Url.js

There is the following, which checks if the regex is matched, and if it is not, it prefixes the input field's value with http://.

if (!f.isValueEmty(value) && !regexp.test(value)) {
    element.domNode.value = 'http://' + value;
    errors.push(this.message.replace('{{ value }}', FpJsBaseConstraint.formatValue('http://' + value)));
}

What I did forget to mention is my validation is triggered onkeyup, so the validation does not run once on submit, but repeatedly as the user enters the URL.

The above code blindly adds the http:// prefix even if it's already present, so you get multiple prefix strings with every keypress.

I think we need an additional check to see if the prefix already exists, and to not add it if it does. Would you agree? Happy to submit a PR if necessary.

benr77 commented 2 years ago

Just submitted #167 to resolve this issue. Let me know what you think.