Julián Yuste
Julián Yuste

Reputation: 1472

entity level validator not triggered

I've observed that when I'm adding an entity to a collection of another entity, the validator for the second entity is not called.

I expected that adding to a child collection triggers entity level validation on the parent when savechanges is called.

I can't write a test right now, but if needed I'll would write it this afternoon.

Is this the expected behaviour or a bug?

entity.OrderLine().arrayChanged.subscribe(function (args) {
        console.log(args);
        if (args.added && args.added.some(function (element) {
            console.log(element.entityAspect.entityState.name);
            return !(element.entityAspect.entityState.isUnchanged() 
                || element.entityAspect.entityState.isDeleted());
        })) {
            console.log("modifico");
            entity.entityAspect.setModified();
        }
        if (args.removed && args.removed.some(function (element) {
             console.log(element.entityAspect.entityState.name);
            return !element.entityAspect.entityState.isAdded();
        })) {
            console.log("modifico");
            entity.entityAspect.setModified();
        }
    });

Upvotes: 0

Views: 229

Answers (1)

Ward
Ward

Reputation: 17863

The parent is not changed automatically by the addition of a child because no data property of the parent changes. That is the technical reason.

Sometimes (often?) the model semantics say that the parent is changed by any addition/deletion/change-to a child. That is not always true which is why Breeze doesn't propagate the change automatically. But it is often true ... and I think it would be a good idea for Breeze to support this for you if you specify this as desired behavior in the metadata. You are not alone in wanting this.

I've added a User Voice suggestion for this idea; please vote for it if it (and add your comments) if it matters to you.

Meanwhile, in an entity initializer you can subscribe to changes in the parent collection and to the items of that collection and have them set the parent to the "Modified" state (parent.entityAspect.setModified()). This will have to do for now I'm afraid.

Upvotes: 1

Related Questions