Url constraint types "http://" with every keystroke
benr77 opened this issue · 3 comments
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://
.
Need more details!
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.