Up Validate
Documentation.Glossary » Validate » Validate on the server

Validate on the server

Last modified on August 15, 2012 17:21

By default, any changes submitted via an EntityManager.SaveChanges call are validated on the server as part of the save life cycle.  

Instance Validation

DevForce will automatically run instance verification on each entity in the save list.  

If there are any validation errors an EntityManagerSaveException is returned to the client.  The FailureType on the exception will be set to PersistenceFailure.Validation , and the error message will be a concatenation of all validation error messages.  The EntitiesWithErrors collection on the exception will also contain the first of any entities which failed validation.  

The "EntitiesWithErrors" is something of a misnomer because only the first error encountered is currently returned.  We did this so that we could add a switch to DevForce at a later date that would cause all entities to be validated before returning and would not need to change, and thus create a breaking change, to EntityManagerSaveException api as a result.  The EntityAspect.ValidationErrors property on the 'failed' entity will also contain the validation errors returned from the server.  If you are binding to these entities in your UI the validation error(s) are displayed as they would with local validation failures (either on the property or instance).

Be sure to wrap a synchronous SaveChanges call in a try/catch, and if calling SaveChangesAsync then be sure to call MarkErrorAsHandled to clear the error.

var op = _mgr.SaveChangesAsync();
op.Completed += (o, args) => {
if (args.HasError) {
  // If validation failed on server, mark as handled and let UI show errors.
  if (args.Exception.FailureType == PersistenceFailure.Validation) {
  // Check other save errors too.
Dim op = _mgr.SaveChangesAsync()
AddHandler op.Completed, Sub(o, args)
   If args.HasError Then
   ' If validation failed on server,
   ' mark as handled and let UI show errors.
     If args.Exception.FailureType = PersistenceFailure.Validation Then
     End If
   ' Check other save errors too.
   End If
 End Sub


Server validation is performed by the EntityServerSaveInterceptor, which you can sub-type to customize save processing.  

The default VerifierEngine used is a single instance of a thread safe "concurrent" VerifierEngine.  Verifiers are discovered and added to the server-side engine in the same way as they are in the client application, but it is not required that the same verifiers be defined on both client and server.  In this way you can define verifiers which will only be executed on the client, or only executed on the server. 

To modify server-side validation, sub-class the EntityServerSaveInterceptor and override the ValidateSave virtual method.

Tags: Validation
Created by DevForce on November 07, 2010 14:15

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade