Skip to content

Conversation

@calweb
Copy link

@calweb calweb commented Dec 15, 2014

...rameter

This current library validates complex objects just fine (issue #224), however, the errors object is not being populated with the errors from the complex objects that are also being validated.

For an example model and view:

 var User = Backbone.Model.extend({
    validation: {
        name: {
            required: true
        } ,
        'address.street': {
            required: true
        }
    }
});

var NewUserView = Backbone.View.extend({
    ...  // template, render, intialize, events
    submitUser: function() {
        var rawUserFormData = {
            name: this.$el.find('input.name').val(),  // empty string
            address: {
                street: this.$el.find('input.street').val(), // empty string
                city: this.$el.find('input.city').val()
            },
            .....
           var newUser = new User();
           var errors = newUser.preValidate(rawUserFormData);

           if(errors) {
               // do something with errors in ui 
               return;
           } 
           newUser.set(rawUserFormData);
           newUser.save();
        };
    }
});

Before this PR, this is what the errors object will look like:

     { 
        'name': 'Name is required'
     }

But afterward, all of the validation items set in the model will populate the errors object:

    { 
        'name': 'Name is required' ,
        'address.street': 'Address. street is required'
    }

I did change the preValidate signature param name (there were a lot of 'value' floating around), but can easily change that back.

@calweb calweb changed the title added ability to use prevalidate with complext objects in a hash as a pa... added ability to use prevalidate with complex objects in a hash as a pa... Dec 15, 2014
@chiefGui
Copy link
Collaborator

the errors object is not being populated with the errors from the complex objects that are also being validated.

Good one, man! We'll be packaging this in the next release.

I just want to ask you to write a simple README markdown about how this new behavior works. Could you?

@calweb
Copy link
Author

calweb commented Jan 15, 2015

@chiefGui thanks!! I'm happy to write a little bit on how this new behavior works. Just to make sure I understand the request, do you want me to edit the existing README for this repository? Or create another file?

@chiefGui
Copy link
Collaborator

The community have to thank you for this contribution, man.

Anyway, create a flat README.md file because a few things will change in the next update(s) then I can't guarantee the file will have the same structure and to avoid conflicts, just create a new one and link it here if possible.

Thank youuu!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants