SilverlightShow: WCF RIA Services Part 6 - Validating Data Comments http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples SilverlightShow.net http://www.rssboard.org/rss-specification Argotic Syndication Framework 2008.0.2.0, http://www.codeplex.com/Argotic en-US estoychev@completit.com (Emil Stoychev) Re: WCF RIA Services Part 6 - Validating Data <p>Yes, I was talking about that :-(.</p> <p>I'm using this to validate textboxes and I want to force a global validation.</p> <p>Thanks anyway ;-)</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment6468 ylogico http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Tue, 30 Aug 2011 15:56:33 GMT Re: WCF RIA Services Part 6 - Validating Data <p>If you are talking about data in the data grid, I do not know how to force it to display the validation errors all the time, it only shows them when the row goes into edit mode.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment6467 brian.noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Tue, 30 Aug 2011 15:55:08 GMT Re: WCF RIA Services Part 6 - Validating Data <p>Thanks for your post!!</p> <p>I'm using DataAnnotation in a Silverlight MVVM application for validation. My problem is that errors doesn't show up until user focus on that field and gets out of it. This can be a problem if user tries to "save" without using every required field for example.</p> <p>Do you know how to force this Validation??</p> <p>Thanks in advance!!!</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment6466 ylogico http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Tue, 30 Aug 2011 12:17:51 GMT RE: WCF RIA Services Part 6 - Validating Data <p>Hi Rahul,</p> <p>Not an issue with RIA Services because all callbacks and Completed handlers are automatically marshaled to the UI thread.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment6038 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Tue, 03 May 2011 02:52:34 GMT RE: WCF RIA Services Part 6 - Validating Data <p>Brian,</p> <p>Are there any cross-thread access issues when code in the callback tries to access the ViewModel properties that are bound to the UI controls because only the UI thread should be manipulating, directly or indirectly, the UI controls. </p> <p> When working with WCF Data Services, I had to use the Deployment....Dispatcher to hook into the UI thread before accessing the UI controls. </p> <p> Is this not an issue with WCF RIA ?</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment6037 RahulMisra2000 http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Mon, 02 May 2011 20:51:09 GMT RE: WCF RIA Services Part 6 - Validating Data <pre><span style="color: #0000ff;"></span> </pre> <pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> BookShelfService : LinqToEntitiesDomainService<BookClubEntities> { [Insert] <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ShareBook(Book book) { ... AmazonService amazonService = <span style="color: #0000ff;">new</span> AmazonService(); <span style="color: #0000ff;">if</span> (amazonService.IsValidASIN(book.ASIN) == <span style="color: #0000ff;">false</span>) { ValidationResult error = <span style="color: #0000ff;">new</span> ValidationResult(<span style="color: #006080;">"The specified Amazon ID did not resolve to an actual book."</span>, <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">string</span>[] { <span style="color: #006080;">"ASIN"</span> }); <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> ValidationException(error, <span style="color: #0000ff;">null</span>, book); } ... } } </pre> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment5900 alman http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Wed, 13 Apr 2011 15:27:52 GMT RE: WCF RIA Services Part 6 - Validating Data <p>I'm new to Silverlight (old dog on ASP.net) so I have been struggling with my overall approach.  I am now comfortable with my current project because you have helped me get a structure.  The download was perfect for me.  I am now enthused and working till 4am again - time passes so quickly doesn't it if only there was overtime?  </p> <p>Keep up the good work and if you are ever in London or in Hampshire UK I will stand you a beer or 2.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment5797 Robin http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Tue, 29 Mar 2011 23:50:28 GMT RE: WCF RIA Services Part 6 - Validating Data Should be able to just add a ValidationResult to the collection of ValidationErrors on the entity base class and set the parameters to the ValidationResult constructor that identifies which properties it is associated with, like the CustomValidation method samples are doing for cross field validation. http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment5719 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Wed, 16 Mar 2011 23:26:59 GMT RE: WCF RIA Services Part 6 - Validating Data For async validation example, as this error is caused by property TotalTime, how to bind this validation to TotalTime, like annotation attribute bind to property in metadata class? In this way, user can see the redbox and message on TextBox for TotalTime. <br /> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment5718 Kent Zhou http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Wed, 16 Mar 2011 20:04:33 GMT RE: WCF RIA Services Part 6 - Validating Data <p><span style="color: #444444; font-family: 'helvetica neue', arial, sans-serif; font-size: 15px; line-height: 19px;">Hello,</span></p> <p><span style="color: #444444; font-family: 'helvetica neue', arial, sans-serif; font-size: 15px; line-height: 19px;">This may interest you and/or your readers : </span><span style="color: #444444; font-family: 'helvetica neue', arial, sans-serif; font-size: 15px; line-height: 19px;">[RIA Services] How to force the validation of my entities (and just the validation !) <a href="http://t.co/MMu241F" data-expanded-url="http://goo.gl/VPHV" target="_blank" rel="nofollow" class="twitter-timeline-link" style="border:0px;margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0084b4; text-decoration: none;">goo.gl/VPHV</a></span></p> <p>Thx for the article by the way !</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4731 Jonathan ANTOINE aka Jmix90 http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Fri, 22 Oct 2010 12:48:05 GMT RE: WCF RIA Services Part 6 - Validating Data <p>Janni, it is also getting cropped and can't follow it as a result. Besides, it is getting a little too specific to your scenario to continue here. Please contact me directly via email (brian dot noyes at idesign.net) and I'll try to help you out.</p> <p>Thanks, Brian</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4544 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 23 Sep 2010 19:40:11 GMT RE: WCF RIA Services Part 6 - Validating Data <p>Hi Janni,</p> <p>You comment is reposted each time you refresh the page. Do not worry I will delete all repeated posts. </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4543 iiordanov http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 23 Sep 2010 18:30:10 GMT RE: WCF RIA Services Part 6 - Validating Data Sorry, I don't know what happened here, why my post is displayed three times? http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4540 Janni http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 23 Sep 2010 18:26:52 GMT RE: WCF RIA Services Part 6 - Validating Data <p>adly the problems start earlier in my case. <br /> This is my Update method on the server DomainService:</p> <p><span style="; margin-left: 0px !important;"><code style="color: #000;">public void UpdateCdGroupDescriptions(cdGroupDescriptions currentcdGroupDescriptions) </code></span></p> <div class="reCodeBlock" style="border:solid 1px #7f9db9;width:;height:;overflow-y:auto;"> <div style="background-color: #f8f8f8;"><span style="; margin-left: 0px !important;"><code style="color: #000;">{ </code></span></div> <div style="background-color: #fff;"><span style=";"><code style=";">    </code><span style="margin-left: 12px !important;"><code style="color: #000;">if (currentcdGroupDescriptions.Title.ToUpper().Equals("BLABLA")) { </code></span></span></div> <div style="background-color: #f8f8f8;"><span style=";"><code style=";">        </code><span style="margin-left: 24px !important;"><code style="color: #000;">ValidationResult error = new ValidationResult("BLABLA wrong!", new string[] {"Title"}); </code></span></span></div> <div style="background-color: #fff;"><span style=";"><code style=";">        </code><span style="margin-left: 24px !important;"><code style="color: #000;">throw new ValidationException(error, null, currentcdGroupDescriptions); </code></span></span></div> <div style="background-color: #f8f8f8;"><span style=";"><code style=";">    </code><span style="margin-left: 12px !important;"><code style="color: #000;">} </code></span></span></div> <div style="background-color: #fff;"><span style=";"><code style=";">    </code><span style="margin-left: 12px !important;"><code style="color: #000;">this.ObjectContext.cdGroupDescriptions.AttachAsModified    </code></span></span></div> <div style="background-color: #f8f8f8;"><span style=";"><code style=";">        </code><span style="margin-left: 24px !important;"><code style="color: #000;">(currentcdGroupDescriptions,this.ChangeSet.GetOriginal(currentcdGroupDescriptions)); </code></span></span></div> <div style="background-color: #fff;"><span style="; margin-left: 0px !important;"><code style="color: #000;">}</code></span></div> </div> <br /> I am generating a ValidationError item for the property Title when it contains a specific string.<br /> Then I throw a ValidationException containing this specific error.<br /> <p></p> <p>On the client side the DomainContext SubmitChanges method looks like that (dummy code):</p> <p></p> <pre style="font-family: consolas;"><pre style="font-family: consolas;"><span style="color: #2b91af;">DataMgr</span>.ArticleContext.SubmitChanges(submitOp => { <span style="color: blue;">var</span> b = 10; }, <span style="color: blue;">null</span>);</pre> <span style="color: #2b91af;"></span><br /> I am using a lambda which checks for errors in the SubmitOperation and displays the error. The problem isthat the code which handles this (ErrorHandler.DiplayError) is not called at all! <br /> <br /> When the server throws a ValidationException as I stated before on the client side I am getting a message box from Visual Studio Just-In-Time Debugger: <br /> "An unhandled exception('Unhandled Error in SIlverlight Application<br /> Code: 4004<br /> Category: ManagedRuntimeError<br /> Message: System.ServiceModel.DomainServices.Client.DomainOperationException:<br /> Submit operation failed validation. Please inspect Entity.ValidationErrors for<br /> each Entity in EntitiesInError for more information.<br /> ...<br /> ...<br /> <br /> This means that I have to somehow handle the exception before it gets rerouted my controls via their data binding? So I changed the code to this:<br /> <br /> <pre style="font-family: consolas;"><span style="color: #2b91af;">DataMgr</span>.ArticleContext.SubmitChanges(submitOp => { submitOp.MarkErrorAsHandled(); }, <span style="color: blue;">null</span>);</pre> Now I am not getting the Exception warning dialog of my visual studio but nothing happens in my GUI. I told the system that I would handle the error by myself.<br /> I tested my code with a TextBox which was bound via a twoway binding and no validation error popped up in my GUI.<br /> <br /> I did not find a complete, working tutorial for this problem on the web. </pre> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4536 Janni http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 23 Sep 2010 18:07:33 GMT RE: WCF RIA Services Part 6 - Validating Data <p>Hi Janni,</p> <p>To a large degree, you don't have to do anything. When you throw that exception on the server, it gets associated inside the RIA Services stack with the entity it was passing to you for updates. When the update batch is completed, RIA Services returns results to the client. Those results include the validation errors that happened on the server for each entity, whether triggered by a validation attribute, or from throwing a ValidationException inside the update methods. On the client side, RIA Services implements the INotifyDataErrorInfo interface for each entity and puts those errors into the collection of errors and raises the ErrorsChanged event. Any bound properties on an entity should then requery by calling the GetErrors method and should display a validation error if the control supports it.</p> <p> That being said, unfortunately the DataGrid only shows validation errors as far as I know if you are editing a row, so it will not show that validation error in the data grid unless you go in and start editing one of the cells for a row who's object has validation errors. If you have normal input controls like TextBox, you should see the validation error immediately. I looked around briefly while writing the article to try to find a way to get the DataGrid to display the validation errors when not in edit mode, but ran out of time and had to pass on that.</p> <p>Hope that clarifies.</p> <p></p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4534 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 23 Sep 2010 16:28:59 GMT RE: WCF RIA Services Part 6 - Validating Data <p><span style="; margin-left: 0px !important;"><code style="color: #000;">Hi,</code></span><span style="; margin-left: 0px !important;"><br /> </span></p> <p><span style="; margin-left: 0px !important;">I am very intrerested in your example for server side validation.<br /> </span><span style="; margin-left: 0px !important;">Especially in the part w</span><span style="; margin-left: 0px !important;">ere you throw a </span><span style="; margin-left: 0px !important;">ValidationException in ValidateCustomerHours.<br /> Your code looks like this:<br /> </span></p> <p><span style="; margin-left: 0px !important;"> <div class="reCodeBlock" style="border:solid 1px #7f9db9;width:;height:;overflow-y:auto;"> <div style="background-color: #fff;"><span style="; margin-left: 0px !important;"><code style="color: #000;">if (!timeAllowed) </code></span></div> <div style="background-color: #f8f8f8;"><span style=";"><code style=";">  </code><span style="margin-left: 6px !important;"><code style="color: #000;">throw new ValidationException( </code></span></span></div> <div style="background-color: #fff;"><span style=";"><code style=";">  </code><span style="margin-left: 6px !important;"><code style="color: #000;">"You cannot enter more than 10 hours per billing cycle for this customer.");</code></span></span></div> </div> </span></p> <span style="; margin-left: 0px !important;"><br /> This code is called in your DomainService in the method U</span>pdateTask(<span style="color: #2b91af;">Task</span> currentTask).<br /> Your example does not use this ValidationException so I prepared a little demo and threw<br /> a ValidationException at the beginning of my UpdateXXX-method in my demo DomainService. <p></p> <p>So far things work, I throw a ValidationException in my UpdateXXX-method in the DomainService and when the DomainContext.SubmitChanges submits an entity which triggers the ValidationException I am getting an exception on my client side.</p> <p>Now how do I translate this specific exception into something that my datagrid or any control that uses bindings can use? How do I tell the corresponding cell in my datagrid or a textbox or whatever control to change its color to red and display the exception error?<br /> <br /> How do I integrate this server side validation exception into something that shows up nicely on the client?<br /> <span style="; margin-left: 0px !important;"><br /> </span></p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4531 Janni http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 23 Sep 2010 11:14:19 GMT RE: WCF RIA Services Part 6 - Validating Data You just did an 9G in your tomcat http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4364 Anand Subramanian http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Tue, 31 Aug 2010 00:39:50 GMT RE: WCF RIA Services Part 6 - Validating Data <p>Ooh, I love insightful readers! :) You caught me in some loose terminology. If it is truly a parent object, you would not be able to get to the object server side because it would not be passed down by RIA Services. However, depending on how you were using the object model and how you decorated it, you could use a Composition attribute on something that was logically a parent (i.e. a Customer property on an Task object) and then it could be passed down. But that would fundamentally modify the way you were managing that object in the domain service so you are not really treating it as a true parent, really more of a child at that point. </p> <p>From a client side though, if the validation was only being used client side (not through the server metadata class for example), that Customer object would be populated still from the domain context setting up the whole object model on the client side and populating the related entity properties, so client side you could walk to the Parent through the related property on the child.</p> <p>Hope that clears it up.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4342 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Fri, 27 Aug 2010 02:52:18 GMT RE: WCF RIA Services Part 6 - Validating Data <p>You wrote "When the validity of one property’s value depends on the value of another property on the same object, or possibly the values of some of the child objects <strong><span style="text-decoration: underline;">or a parent object</span></strong>, you can accommodate this fairly easily using custom validation"</p> <p> </p> <p>How do you get the "parent object" (not "the same object") of a property from ValidationContext?</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx#comment4335 eukal http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx Thu, 26 Aug 2010 13:02:14 GMT