Knockout-Validation
Knockout-Validation copied to clipboard
When specifying the maxlength of a field, add attribute maxLength to the element
How would i do this in Knockout validation? When specifying the maxLength of a input through the validation, it will properly validate, but I think it'd be a good idea to add the maxLength attribute to the HTML element itself, which would prevent (in realtime) the user from ever going over the maxLength. (http://www.w3.org/TR/html401/interact/forms.html#adef-maxlength)
This functionality is not build in, so is there any way for me to add some customization to KO validation to achieve the ability to add the maxLength attribute to an input?
You would be able to do this using the attr
binding built in to knockout itself.
Personally I'm not sure the functionality should fall within the scope of Knockout-Validation though; library is meant to evaluate validations and display warnings/errors, whereas what you are suggesting would actually prevent the user from exceeding the specified max length so no message and no re-evaluation would ever be required...
@stevegreatrex
I agree with the logic of your position, but in practice, maxLength is an issue of validation, even up in the UI. Consider the following statement:
"Validation is validation only when an error message will be displayed to the user."
I don't know if I agree with this statement, which is essentially what you're saying. We could extend this informal definition of validation in the following way:
"Validation is validation when we react to erroneous input from the user, programmatic input from the system itself, or when we proactively prevent invalid input in the first place."
I modified your code to include the maxLength attribute in the html5Attributes array without any apparent side effects. Would you consider exposing an API, or an option on the configuration, for extending HTML5 attributes? Perhaps let us decide.
In any case, you guys have done a great job!
Thank you.