Elisabeth
Elisabeth

Reputation: 21206

Can breezejs validate a newly created entity on property change NOT on SAVE

from the breezejs manual:"Breeze automatically validates entities in cache. It won’t do so for detached entities. For example, a newly created Customer is technically invalid because its CompanyName is null and that property is required. Breeze does not validate the customer until you add it to the cache. This gives you time to set the values of a new entity before it enters the cache and triggers validation."

My question is in the above context:

The samples floating in the web about breezejs and validation always show validation when a new entity is about to be created AND the SAVE button is pressed.

Can breezejs also validate property changes when I tab from one input field enter 100 chars (10 are allowed) and tab to the next input field? I just want to show my validation error message on each property change NOT when the user hits the Save changes button.

Or does this not work for a new created entity as it is not yet added to the cache? Does that only work for entites edited?

Upvotes: 4

Views: 1231

Answers (2)

adamlj
adamlj

Reputation: 225

An entity is added to the cache by default. When you are creating an entity you can explicitly set its entityState to Detached or any other state if you would like. (entityManager.createEntity('entityType', null, breeze.EntityState.Detached))

As for validation, you can manually validate entities in any state, at any time. If you want to check validation when a property is changed, simply subscribe to the entityAspect.propertyChanged event. The propertyChanged event will give you access to the entity itself, the name of the property that changed, the old value and the new value. Then you simply have to check the properties validation using entity.entityAspect.validateProperty(propertyname) and handle the results.

Subscribe to property changes and validate manually
(Code below is combined from breeze examples)

// assume order is an order entity attached to an EntityManager.
order.entityAspect.propertyChanged.subscribe(
function (propertyChangedArgs) {
    // this code will be executed anytime a property value changes on the 'order' entity.
    var entity = propertyChangedArgs.entity; // Note: entity === order
    var propertyNameChanged = propertyChangedArgs.propertyName;
    var oldValue = propertyChangedArgs.oldValue;
    var newValue = propertyChangedArgs.newValue;

    if (!entity.entityAspect.validateProperty(propertyNameChanged)){
        //Property is not valid. Inform the user if needed.
    }

});

For more help and examples see these links's:
Subscribing to the propertyChanged event
Manually validating a property
Creating a new entity

Upvotes: 4

jvrdelafuente
jvrdelafuente

Reputation: 2002

I think that it is possible, you can do manual validation.

if (!newCustomer.entityAspect.validateEntity()) {/* do something about errors */}
You can also validate a specific property:

if (!newCustomer.entityAspect.validateProperty("CompanyName")) {
       /* do something about errors */} 

I have get these examples from: http://www.breezejs.com/documentation/validation

I hope this will help you.

Upvotes: 1

Related Questions