locked
How does one display RIA validation errors for deleted entities? RRS feed

  • Question

  • If I need to delete a JobPosition from a collection of JobPositions for a Company, I first need to verify that there are no employees currently holding that position.  The client only has references to Companies and JobPositions ... not employees.  I've tried the following scenarios but each seems to really provide a good user experience.

    1.  Place a JobPositionsValidator on the server side on the DeleteJobPosition service method.  The problem here is that even though a ValidationResult is attached to the Entity, because the JobPosition Entity has been deleted on the client, the ValidationSummary is unable to display the ValidationError.

    2.  Same as 1 but I do the following on the client side when I get the SubmitOperation result:

                _context.SubmitChanges((qr) =>
                    {
                        if (qr.HasError)
                        {
                            foreach(var entity in qr.ChangeSet.RemovedEntities.Where(e => e.HasValidationErrors))
                            {
                                ((IRevertibleChangeTracking)entity).RejectChanges();
                            }
                            qr.MarkErrorAsHandled();
                        }
    
                    }, null);
    

    This worked in that I got a validation error and restored the entity, but Validation Summary still did not display the ValidationError of the Entity.  Also, this winds up causing problems when my ViewModel tries to select the "restored" entity and I haven't been able to figure out what's happening there yet.

    3.  Validate based on an Invoke before deleting the Entity on the client.  This would work, except that the user may not submit changes immediately.  I would still need to perform the validation server side when the SubmitChanges operation is called.  By then, the validation may not pass and I'm back to my previous scenarios where I don't seem to have a good way of displaying the ValidationError

    How does one typically validate entities before a Delete?  Are you able to use ValidationSummary with delete validation errors?

    Thanks for your help on this!

    Wednesday, August 31, 2011 2:19 PM

Answers

  • I'm not sure the ultimate experience that you are looking for, but rather than call RejectChanges() on the Entity, can you add it back to the EntitySet (EntitySet.Add(entity)) if there are Validation Errors during the Delete? That should put the Entity back into the EntitySet in an Unmodified state, while keeping the ValidationError. It will also clear out the 'Delete' operation from the ChangeSet.

    I think the problems you are seeing are:
    - RejectChanges() only rejects property changes on the Entity. It does not affect Added/Deleted state. You can see this if you look at EntityState before/after you call RejectChanges() -- both times it is Deleted.
    - Because the Entity is Deleted, that means that it does not exist in the EntitySet, which may be why you are unable to select it through your ViewModel.

    Thursday, September 1, 2011 7:08 PM

All replies

  • How the entity is deleted in the client? Is it by datafirm or manualy in the code? Are you using RIA domain datasource?

    Thursday, September 1, 2011 12:55 PM
  • Sorry, I should've noted.  My POCO object is defined like so:

    public class Company
    {
       [Key]
       public int Id {get; set;}
       ...
    
       [Include]
       [Composition]
       [Association("CompanyJobs","Id","OwnerId")]
       public ICollection<Job> Jobs {get; set;}
    }
    
    public class Job
    {
       [Key]
       public int Id {get; set;}
       ...
    
       public int OwnerId {get; set;}
    }
    

    On the client, I simply do the following:

    //This code is all part of my ViewModel
    private Company _selectedCompany;
    public Company SelectedCompany
    {    
        get { return _selectedCompany; }
        set {
              if(_selectedCompany != value) {
                  _selectedCompany = value;
                  RaisePropertyChanged("SelectedCompany");
              }
    
    }
    
    ...
    
    public void RemoveJob()
    {
        SelectedCompany.Jobs.Remove(SelectedJob);
    }

    Because this is a composition object, the parent Company is flagged with HasChanges = true.

    As I said, SubmitChanges() works and the validation works on the server side.  The validation results are passed back, but there is really no way to have them displayed because the validation result is tied to the "deleted" entity.

    Thursday, September 1, 2011 2:54 PM
  • I'm not sure the ultimate experience that you are looking for, but rather than call RejectChanges() on the Entity, can you add it back to the EntitySet (EntitySet.Add(entity)) if there are Validation Errors during the Delete? That should put the Entity back into the EntitySet in an Unmodified state, while keeping the ValidationError. It will also clear out the 'Delete' operation from the ChangeSet.

    I think the problems you are seeing are:
    - RejectChanges() only rejects property changes on the Entity. It does not affect Added/Deleted state. You can see this if you look at EntityState before/after you call RejectChanges() -- both times it is Deleted.
    - Because the Entity is Deleted, that means that it does not exist in the EntitySet, which may be why you are unable to select it through your ViewModel.

    Thursday, September 1, 2011 7:08 PM