cannot convert to object

Posted by Dazz on Stack Overflow See other posts from Stack Overflow or by Dazz
Published on 2011-01-12T14:48:33Z Indexed on 2011/01/13 9:54 UTC
Read the original article Hit count: 236

Filed under:
|

if i execute the following code i get a cannot convert to object error; Uncaught exception: TypeError: Cannot convert 'validation.messages.field' to object

$.fn.validate = function(validation) {
    $.each(validation.rules, function(field, fieldRules){
        $.each(fieldRules, function(rule, ruleValue){
            var fieldValue = $('[name=' + field + ']').val();
            if (eval(rule + '(fieldValue, ruleValue)') == false){
                alert(validation.rules.field.rule);
                return false;
            }else{
                return true;
            };
        });
    });
}

the problem is the

alert(validation.messages.field.rule);

'field' = 'persoon_voornaam' and 'rule' = 'required' and validation.messages.persoon_voornaam.required works just fine.

What am i doing wrong?

validation is a JSON that look like this:

{
    rules: {
        persoon_voornaam: {
            required: true, 
            minlength: 5,
        },
        postcode_bestemming: {
            required: true, 
            minlength: 7,
        },
    },
    messages: {
        persoon_voornaam: {
            required: 'Dit veld is verplicht',
            minlengt: 'Dit veld moet minstens 5 lang zijn', 
        },
    }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON