backbone.validation icon indicating copy to clipboard operation
backbone.validation copied to clipboard

added ability to use prevalidate with complex objects in a hash as a pa...

Open calweb opened this issue 11 years ago • 3 comments

...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 avatar Dec 15 '14 08:12 calweb

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?

chiefGui avatar Jan 15 '15 17:01 chiefGui

@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?

calweb avatar Jan 15 '15 18:01 calweb

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!!

chiefGui avatar Jan 15 '15 19:01 chiefGui