Validators don't work with number input types
At least the presence and format validators do not work when the input type is a number. Both validators do a check for a string as part of their validation, which works ok when passing a number to a text input because the number becomes a string, however it does not work when the input type is a number because the number remains a typeof number.
In instances where the input type is number, the validator should check for typeof number rather than string.
Looking at the code for the presence validator, it should only tick if the value being validated is
nullorundefined- a
Stringthat is empty or consists of only whitespace - an empty array
- an empty object
So, unless your input is throwing one of those values, the presence of the value should always validate to true.
In case you really want to use the format validators, which are based on RegExp testing that is inherently meant to work on strings only, for your number inputs, I would recommend casting the values coming out of your inputs into strings before validating.
Also check out Numericality validators, which might prove useful!
Should the presence function not look for numbers too? This function is assuming that any number value is not present. So if an input type is number, the presence function will not work for that input unless the number is converted to a string. This seems like an omission to me. Numbers are a legitimate form input so shouldn't need special treatment unless there is a good reason?
I think the format function should also be able to handle numbers but am not completely attached to this if there is good reason not to. However I do again think that having to handle number inputs on every input, or with a custom function, is unnecessary and could be built in to the source code.
This seems to be working as expected:
validate({}, {foo: {presence: true, numericality: true}});
// => "{"foo":["Foo can't be blank"]}"
validate({foo: "Hello"}, {foo: {presence: true, numericality: true}});
// => "{"foo":["Foo is not a number"]}"
validate({foo: 3}, {foo: {presence: true, numericality: true}})
// undefined
Yes but when attempting to validate a input type of "number" it doesn't work because it validates against a string and not a number. Number input types are incompatible
Please share an exemple of your problem
This code will pass the validation as expected, because the input has a value and is therefore present.
<input name="foo" type="text" value="5">
foo: {
presence: true
}
This code will not pass the validator even though the input has a value and is therefore present. The only difference between this and the first example is that the input type is a number and therefore the value type is a "Number" rather than a "String". I believe validate.js does a check for the type of value passed from the input, and does not check for a type of "Number". So in the first example, where the value is a type of "String", the validation passes, because strings are ok, whereas in this example, numbers are not checked for and therefore the validation fails.
<input name="foo" type="number" value="5">
foo: {
presence: true
}
I've just discovered this is also true for the format validator. Again, validation will work for text inputs but not for numbers. If I pass the following pattern to the format validator; /[0-9][0-9]/ with a "number" input and the value 11 it will not pass validation. If I pass the same pattern to a "text" input with the value "11" then it passes.
Why don't to cast to string before validation? All string related rules could perform such casting. Especially for primitive types.