thedersen/backbone.validation — Issue #30: Provide more information to 'validated' event subscribers
Author: ryantofteland
Created: 2012-03-07 16:26:10+00:00
State: open

I'm impressed with your validation plugin and how active you've been in updating and improving it. I like how you've given the flexibility to use the View specific functionality or just the model validation itself.

In my case I'm using just the model validation functionality and subscribing to the `validated` event that's triggered in the `validate` method defined within the `mixin`. This works well although it would be nice if more information was passed with the event. Currently only the `result.invalidAttrs` are provided. This makes it difficult to determine which rule failed in cases where a given attribute has multiple rules defined on it (Ex: FirstName has a `minLength` and `maxLength` rule) - and therefor impossible to get the correct error message.

There's probably a couple of ways to solve this. This simplest might be to pass the entire `result` object with the triggered event instead of just the `result.invalidAttrs`. I could then match up the error message to the invalid attribute. This would be a breaking change though - so I'm not sure how acceptable that is.

**Example:**

model.trigger('validated', model._isValid, model, result.invalidAttrs);
model.trigger('validated:' + (model._isValid ? 'valid' : 'invalid'), model, result.invalidAttrs);

Long term passing that result object could open the door to tracking multiple failed rules/messages per attribute and then be available to the event subscribers - but I didn't want to go down that road quite yet. That's probably a bigger question and change.