-
Notifications
You must be signed in to change notification settings - Fork 379
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
maxLength fails if observable happens to be a number rather than a string #457
Comments
I'm not sure that it should be the responsibility of this library to cover If you open a PR with tests etc then I'll get it merged in.
|
it is not quite as simple as adding an empty string/char to the front of the object as you accept arrays and other possible objects, so I think the safest approach is to check if length is undefined after the isEmpty check then if it doesnt have a length try adding the string, or adding some form of isNumber to the utils by doing an inverse NaN check or something, then if it is prefix the string. |
I have made proposed changes on c9 and run tests: https://ide.c9.io/grofit/knockout-validation If you connect up and just run grunt you should see all tests pass, 2 new ones were added to min and max length specifically to check for numbers with the issue number. To reduce risk I have added an Take a look and if you are happy I will commit it up to my fork and push. |
Sorry, I don't have a c9 account; if you push it to github I'll take a look On 9 October 2014 18:53, Lee Prosser notifications@github.com wrote:
|
I can see the next comment being "It is not meant for numbers", which is 100% correct and I understand that. However I have a scenario where a user can enter up to 20 characters into an observable, however more often than not they would put in a number like "20".
Now in most cases this is fine and is processed as text, however it seems that upon rehydration of data I assume knockout is treating it as a number 20 not the text 20 which causes validation to fail.
Now the failure is because in this case when maxLength check does:
val.length >= minLength
it will return false asval.length
is undefined.Although I am raising this issue here I am not sure if it is this libraries job to handle this use case, however I cannot tell how it is getting converted from a string to a number as it just an ajax request down from the server with the data, so it just populating like any other means
myObservable(someValueFromServer)
.I only raise it because in JS we do not enforce type so it could be possible for you to have something that you want to have a maxLength on if it is string but if it is not then dont apply it, so I was wondering if maybe it should be checked to see if length is not undefined first then try that check, if it is undefined then maxLength should not apply... however again I am not sure if that is the right stance...
I wanted to raise it anyway as it has been giving me issues for while but it was only today I realised what the issue was with it reading the text as a number on rehydration. I can work around it but wanted to know your thoughts.
The text was updated successfully, but these errors were encountered: