开发者

ria services validation

开发者 https://www.devze.com 2023-02-12 06:47 出处:网络
I\'m train to do the validation of my entity on the client, but it does not work. I\'ve \"required\" and range开发者_高级运维 and stringlength attributes. The problem is that only required is validati

I'm train to do the validation of my entity on the client, but it does not work. I've "required" and range开发者_高级运维 and stringlength attributes. The problem is that only required is validating on the client side. I have à validate method on the client before I call SubmitChanges. But only required attribute is validated against. Then SubmitChanges is called and raises an exception, because I still have range or stringlength validation errors. I'm using TryValidateObject: is it ok?

Please help :-)


When you call SubmitChanges it returns a SubmitOperation which has a property called HasError. If this is true then it means one or more of the entities are in Error. You can also use the EntitiesInError Property that will bring you back all the Entities with any errors including validationErrors

TestDomainContext tdc = new TestDomainContext();
SubmitOperation so = tdc.SubmitChanges();
if (so.HasError)
{
  foreach (Entity entity in so.EntitiesInError)
  {
      if (entity.ValidationErrors.Count() > 0)
         //Loop through validation errors to see what property is in error
  }
  so.Cancel();
}

This gives you more control over the errors than doing the TryValidate. You can also then cancel the SubmitOperation if it has errors...


The validation could not occur, because TryValidateObject only performs "Required" validation. We have to set the last parameter to true to validate all validation types (Range Stringlength ...). For sure validating in the SubmitChanges method is also not bad and it not go on the server for the validation if it can validate on the client-side already.

0

精彩评论

暂无评论...
验证码 换一张
取 消