jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

JavaScript runtime error: Unable to get property 'substr' of undefined or null reference #144

Closed philvbprogrammer closed 7 years ago

philvbprogrammer commented 7 years ago

Hi I keep getting this when running my MVC 5 page

Exception was thrown at line 492, column 13 in http://localhost:36849/Scripts/expressive.annotations.validate.js 0x800a138f - JavaScript runtime error: Unable to get property 'substr' of undefined or null reference

which refers to:

   getPrefix: function(value) {
            return value.substr(0, value.lastIndexOf('.') + 1);
        }

the value passed is null so fails, is this a bug or what do i need to do in order to pass a value to getPrefix?

Thanks Phil

philvbprogrammer commented 7 years ago

actually it was undefined. i modded my js to be

 getPrefix: function (value) {
            if (value !== undefined && value !== null) {
                return value.substr(0, value.lastIndexOf('.') + 1);
            }

and this has fixed it

jwaliszko commented 7 years ago

While this is a reasonable fix, it does not explain the source of the problem - mainly why there is no names on your input fields? EA demands the names to exist - based on them, when needed, it is able to lookup for a particular field and extract value of it.

philvbprogrammer commented 7 years ago

aha, the name does exist in the HTML markup, BUT it is a custom strongly-typed htmlhelper i wrote which uses a div as the container then multiple checkboxes inside. due to it being a div you cannot access the name attribute via element.name....mmmm so how can i do this....as a span would have the same issue. any ideas?

Phil

jwaliszko commented 7 years ago

I don't know what you're trying to achieve since you've provided no details.