Backbone Validation with complex objects

239 Views Asked by At

I'm having trouble getting Backbone.Validation to handle complex object validation. per the documentation:

Validating complex objects is also supported. To configure validation rules for objects, use dot notation in the name of the attribute, e.g 'address.street'.

I took the example fiddle and added a complex object but I can't get it to work.

var SignUpModel = Backbone.Model.extend({
    defaults: {
        terms: false,
        gender: '',
    },
    validation: {
        username: {
            required: true
        },
        email: {
            required: true,
            pattern: 'email'
        },

        // complex object
        'address.zip': {
            required: true
        },

It looks like the dot notation in the name on the input element because I'm getting this error:

Uncaught Error: Syntax error, unrecognized expression: [name=address.zip] 

Have a look at the fiddle here.

1

There are 1 best solutions below

4
On

If i'm not wrong the attribute name don't accept (.), So you could not have this name. But i'm not shure.

When the documentation says that "Validating complex objects is also supported. To configure validation rules for objects, use dot notation in the name of the attribute, e.g 'address.street'". I guess that address have an attribute called street. and it really makes sense.

Changing the attribute name to "address-zip" wil work.

hope it helps.

more reference about attribute names here. What characters are allowed in an HTML attribute name?