0

Is there a way to override ModelState.IsValid?

Some of the entities to be validated are just attached entities, so all the fields except the ID are not to be validate as the entity is in state Unchanged.

Is there a way to do this?
Has anyone faced this issue before?

Update

Say I have the following action:

[HttpPost]
public ActionResult SaveEntity(MyEntity entity)
{
    var isValid = ModelState.IsValid; //false
}

Since the model validates all properties and all descendant properties of entity, there has to be a way to check on each entity of those descendants, whether it's attached to the context, and if it is, remove error from ModelState, something like the following:

public ActionResult TryValidateDetachedModel(MyEntity entity, DbContext context)
{
    foreach (var ms in ModelState.Where(ms => ms.Value.Errors.Any()).ToArray())            
    // should iterate over something like GetAllEntityTypesMetadata()
    {
        var entity = GetEntityFromMetadata(ms);
        if (context.Entry(entity).State == EntityState.Unchanged)
        {
             ms.Value.Errors.Clear();                
        }
    }
}

What I'm trying to do in the above pseudo code is to check the entities in the validation chain, and if one of them is attached as Unchanged, skip validation / remove its errors.

Right now I have to do it hard-coded manually by checking ModelState.Key, I'm looking for a more generic and efficient way.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • Why do you bind to EF object, not designated view models? – LukLed Jul 14 '13 at 10:11
  • I'm using POCO entities, so it makes no difference. – Shimmy Weitzhandler Jul 14 '13 at 10:44
  • Obviously it makes a difference, because with dedicated view models you wouldn't have this problem. – LukLed Jul 14 '13 at 10:48
  • OK, assume you're right. Currently I can't afford to recreate view-models for all the models in my domain. Can you think of a solution that incorporates interaction with a provided `DbContext` to see what entities are attached as `Unchanged` and skip validation / remove errors for them? – Shimmy Weitzhandler Jul 14 '13 at 10:59
  • Ok. I am not really sure what you are asking for. How is it possible, that entities are attached to DbContext after binding? Maybe you can write some king of `ModelBinder`, that checks state and ignore validation when entity is unchanged, but I am not really sure what you are asking for. – LukLed Jul 14 '13 at 16:27
  • No. I have another method that automatically attaches the relevant associated FKs to the context. – Shimmy Weitzhandler Jul 14 '13 at 17:40

2 Answers2

1

To clear all errors use next

ModelState.Clear();

regards

tratatun
  • 135
  • 1
  • 8
0

Here's what I do to ensure the validation only applies to the current entity:

        foreach (var key in ModelState.Keys)
            if (key.Split('.').Length > 2)
                ModelState[key].Errors.Clear();

        if (!ModelState.IsValid)
            return BadRequest(ModelState);

The check for the occurrences of . means: if the modelstate key is something like currentDTO.relatedDTO.field then that validation error is ignored (cleared). If it's just id or currentDTO.validateThisField, then it doesn't get cleared.

Sean
  • 14,359
  • 13
  • 74
  • 124