Validation

There are 30 entries for the tag Validation
A Fun ValidationAttribute Bug

I tweeted about a bug that I recently helped fix in System.ComponentModel.DataAnnotations.ValidationAttribute. As I said, it's a bug resulting from code I wrote for that class years ago. I was honestly surprised there was any interest in this, but there was! Since I piqued your interest, I thought it only fair that I quench your thirst and show you the details of the bug. I helped fix a bug in System.ComponentModel.DataAnnotations.ValidationAttribute today. A threading bug I introduced a long time ago. :-(— Jeff Handley (@JeffHandley) April 15, 2014 History and Backwards Compatibility The first implementation of ValidationAttribute had a method with the...

posted @ Tuesday, April 15, 2014 1:29 AM | Feedback (2)

RIA Services Validation: Available on GitHub

I’ve gotten quite a few requests for the source code behind the RIA Services Validation blog post series.  I am pleased to announce that the code is now published on GitHub. http://github.com/jeffhandley/RIAServicesValidation Within that repository, you’ll find: RudeValidation.Web/Models – Server-side model classes RudeValidation.Web/Validators – Custom validators including CompareValidator, ConditionallyRequired, and DateValidator RudeValidation/Views – Sample screens showing the validation in action RudeValidation/ViewModels - Where you’ll find the sample ViewModel as well as the very simple ViewModelBase If you’re not familiar...

posted @ Tuesday, September 6, 2011 11:50 PM | Feedback (2)

RIA Services Validation: ViewModel Validation with Entity Rules

For those of you familiar with the ViewModel (or MVVM) pattern, you are likely also familiar with a typical pain point regarding validation: you often need to duplicate your entity validation metadata onto your ViewModel classes.  This can lead to burdensome dual maintenance of your validation rules, and it can seem very frustrating that with the server to client metadata propagation that RIA Services offers, your ViewModel classes are left dangling out there for you to manage yourself.  In this post, I’ll illustrate a utility I created that allows a ViewModel to assume validation metadata from model classes or properties,...

posted @ Tuesday, September 6, 2011 11:31 PM | Feedback (5)

RIA Services Validation: Video Tutorials (in VB)

Not too long ago, a gentleman named Bill Burrows (a VB MVP) contacted me and asked for permission to use content from my blog to create a video tutorial series on RIA Services Validation.  I was really flattered by this and gave him my permission.  I apply a Creative Commons Share-Alike license to the content on my blog, so Bill could have really done this without my explicit permission, but I do appreciate that he asked.  I simply requested that he respect the Share-Alike and Attribution portions of the license. Today, Bill sent me an update to let me...

posted @ Friday, November 19, 2010 9:43 PM | Feedback (0)

RIA Services Validation: Using ValidationContext (Cross-Entity Validation)

In our last post, we learned how ValidationContext can be used to provide and consume state and services for validation methods to use.  Now, let’s take a quick look at how validation methods can actually use this information.  I’m going to include a bunch of code in this post, and I will rely on the code to be rather self-explanatory. Property-Level Validation In this property-level validation example, we need to validate that the location entered for a meeting is a valid location from our database.  We want this to work both on the server and on the client,...

posted @ Monday, October 25, 2010 1:56 AM | Feedback (20)

RIA Services Validation: Providing ValidationContext

Throughout this series on RIA Services Validation, I’ve mentioned and shown ValidationContext several times.  In this post, we’ll learn about the purpose of ValidationContext, what types of context it can provide, and how it integrates with RIA Services.  This will allow us to explore further topics such as cross-entity validation and open the door for other advanced validation scenarios. ValidationContext Purpose Although ValidationContext exists within the System.ComponentModel.DataAnnotations assembly in both .NET 4.0+ and Silverlight 3+, the class was introduced by the RIA Services team.  While designing the RIA Services validation system, we considered scenarios where validation logic is...

posted @ Monday, October 25, 2010 12:49 AM | Feedback (7)

RIA Services Validation: Entity-Level Validation

In my last post, I went over some cross-field validation scenarios and provided some sample code, including a CompareValidatorAttribute.  We’ve now covered single-field validation and cross-field validation, but there’s yet another level of validation supported by RIA Services—Entity-Level validation.  As you’ll see in this post, entity-level validation is very similar to what we’ve already seen. Entity-Level Validation Declaration Your entity types can declare entity-level validation using either the [CustomValidation] attribute approach where a custom validation method is supplied, or by having an attribute that derives from ValidationAttribute.  Either way, to indicate that a validation rule applies at the...

posted @ Tuesday, October 12, 2010 1:31 AM | Feedback (7)

RIA Services Validation: Cross-Field Validation

I frequently hear questions about how to perform cross-field validation in RIA Services.  Before thoroughly covering this topic*, I wanted to be sure to go through some simple scenarios, show how to use CustomValidationAttribute, how to derive from ValidationAttribute, explain how validation rules are propagated to the client, and what triggers the validation.  Hopefully by now, you’re getting pretty comfortable with the validation framework and you’re ready to explore some more examples. * Perhaps you noticed that I snuck a reusable cross-field validator into the Custom Reusable Validators post—it’s a rather useful ConditionallyRequiredAttribute. In this post, I’m going...

posted @ Sunday, October 10, 2010 1:29 AM | Feedback (4)

RIA Services Validation: Validation Triggers

My goal with this blog post series is to provide an end-to-end tour of how RIA Services validation works and how you can leverage its capabilities to build rich, responsive applications.  A key factor for making the most of validation is fully understanding when and how RIA Services invokes your validation rules.  Awareness of the various validation triggers will help you implement your validators so they are fired as you expect, when you expect.  In this post will dig into the RIA Services codebase and identify every place where entity validation is invoked. Client-Side Validation Property Setters –...

posted @ Wednesday, October 6, 2010 3:48 AM | Feedback (2)

RIA Services Validation: Attribute Propagation

Since the beginning of the RIA Services project, a primary tenet was to propagate validation rules defined on the server up to the client, integrating the validation directly into the Silverlight UI.  This came to fruition with RIA Services v1.0 and you can in fact apply an attribute to your model on the server and automatically get instant client-side validation of data entry on even the simplest of Textbox controls.  In fact, some validation rules are inferred from your data model directly.  This is a feat that many other frameworks have attempted to accomplish, but I believe RIA Services has...

posted @ Thursday, September 30, 2010 2:37 AM | Feedback (2)

RIA Services Validation: Custom Reusable Validators

We just covered Custom Validation Methods, where we learned how to use CustomValidationAttribute to invoke a static (VB: Shared) method to perform validation.  Let’s talk about an alternate approach to custom validation though: creating custom, reusable validators by deriving from ValidationAttribute.  While it’s true that the custom validation methods used by [CustomValidation] could certainly be reusable, I tend to think of that approach as a light-weight way to call specific business logic routines for validation.  The approach we’re about to see is what I use when I’m creating an inventory of validators to be reused throughout my project(s). Deriving...

posted @ Sunday, September 26, 2010 3:39 AM | Feedback (7)

RIA Services Validation: Custom Validation Methods

In our last installment, we learned about the standard validators that ship in the System.ComponentModel.DataAnnotations assembly.  RequiredAttribute, RangeAttribute, StringLengthAttribute, and RegularExpressionAttribute all derive from ValidationAttribute and cover many common validation scenarios.  Every system has its own business rules though, and therefore it’s imperative that RIA Services allows you to create custom validators to implement your own logic.  Moreover, your custom error messages must plug into the same framework that powers the standard validators.  To address this requirement, we added CustomValidationAttribute. Using CustomValidationAttribute to Invoke a Method System.ComponentModel.DataAnnotations.CustomValidationAttribute allows you to easily redirect a validator to a method you’ve...

posted @ Saturday, September 25, 2010 11:12 PM | Feedback (2)

RIA Services Validation: Standard Validators

RIA Services offers powerful features for flowing validation from your entity model all the way up to the Silverlight UI.  In fact, flowing your validation rules from the server into your form controls was one of the primary tenets of RIA Services V1, and our team did the work of getting validation integrated deeply into Silverlight, long before RIA Services was released.  In this post we’ll take a look at the standard validators that are offered. System.ComponentModel.DataAnnotations.ValidationAttribute RIA Services employs the ValidationAttribute class from System.ComponentModel.DataAnnotations for defining validation rules on your entity model.  ValidationAttribute itself is abstract (VB:...

posted @ Wednesday, September 22, 2010 3:16 AM | Feedback (4)

Async Validation with RIA Services

Almost a year ago, I posted a lengthy article on how to implement Async Validation with RIA Services and DataForm.  It was hard; much harder than it should have been, and we all knew it.  Since then, things have changed, and async validation is a lot simpler.  Let’s take a look at why it’s easier and how you can easily perform async validation in your application. INotifyDataErrorInfo Silverlight 4 shipped with an interface called INotifyDataErrorInfo, and controls that previously supported validation by way of exceptions now support async validation through this interface.  This laid the groundwork for RIA...

posted @ Wednesday, May 26, 2010 1:06 PM | Feedback (32)

ValidationResult - Specifying Member Names

The System.ComponentModel.DataAnnotations.ValidationResult class was created for RIA Services, and it was introduced in Silverlight 3.0 and then added into .NET 4.0.  A ValidationResult represents a validation error condition, specifying the error message and, optionally, any member names that the error corresponds to.  If you use the [Required] attribute, the [Range] attribute, [StringLength], or any of the other provided ValidationAttribute classes, then ValidationResult instances are created to represent any validation failures.  If you have used the [CustomValidation] attribute or if you have derived from ValidationAttribute yourself, you have likely returned an instance of ValidationResult in your code, or ValidationResult.Success if validation...

posted @ Wednesday, February 24, 2010 11:42 PM | Feedback (7)

Validating Objects and Properties with Validator

ASP.NET Dynamic Data introduced the System.ComponentModel.DataAnnotations namespace in .NET 3.5 SP1.  The namespace contained a bunch of attributes for applying validation rules to objects and their properties.  With the “Alexandria” project (which morphed into .NET RIA Services plus some Silverlight/SDK/Toolkit additions), we were exposing your server-side entities up to your Silverlight client.  In doing this, we wanted to preserve your DataAnnotations attributes on the client, which of course meant that we needed a Silverlight version of this assembly. (See Also: Sharing Source with Silverlight). As our team was creating this Silverlight assembly, we found that we needed...

posted @ Friday, October 16, 2009 5:40 PM | Feedback (16)

Not Breaking on ValidationException (take 2)

Awhile back, I blogged about how Silverlight uses Validation Exceptions, which can cause the debugger to break.  I showed how to prevent that from happening, but the solution was less than ideal. Someone named rlodina commented on the post: What about manual adding: System.ComponentModel.DataAnnotation.ValidationException in this list. This comment has been sitting in my inbox for about a week, and I had wanted to follow up on this.  Well good gosh, this is easy and it works like a charm.  You can in...

posted @ Friday, August 7, 2009 7:44 PM | Feedback (6)

Async Validation with .NET RIA Services and DataForm

Please note… this post is out-dated and all of the madness below is no longer needed in order to implement Async Validation with RIA Services.  Please see the new post for the current approach. The Birth of QuickSilverlight.Validation Over on the .NET RIA Services forums, “SilverlightRIA” asked about performing asynchronous validation with .NET RIA Services and the Silverlight Toolkit’s DataForm.  He wanted to intercept the Commit from the DataForm and invoke his async validation, integrating the results into the DataForm’s validation UI.  This was an interesting scenario so I put some time into a solution for it this week.  The result is...

posted @ Saturday, July 11, 2009 5:05 AM | Feedback (9)

Validating Decimal Ranges with RangeAttribute

System.ComponentModel.DataAnnotations offers a RangeAttribute that can be used to validate that a value is within a given range.  Silverlight 3’s SDK includes this assembly and .NET RIA Services and ASP.NET Dynamic Data use this too. Something that a few of us have stumbled on here internally is that the RangeAttribute doesn’t have a constructor that takes Decimal values.  In fact, a bug was submitted by another feature crew for this.  Unfortunately, even if RangeAttribute offered a constructor that accepted decimals, you still wouldn’t be able to pass them in. Here’s what the code would look like: ...

posted @ Thursday, July 9, 2009 2:39 PM | Feedback (0)

Validation Exceptions in Silverlight

With the Silverlight 3 Beta SDK and .NET RIA Services, a lot of people are starting to utilize the System.ComponentModel.DataAnnotations library to add validation metadata to their entities.  Something that has tripped up some people is the use of exceptions for validation errors, where Visual Studio breaks with a user unhandled exception. As Keith Jones reported, Silverlight 3 uses exceptions to notify controls when validation has failed.  This means that many users will see Visual Studio break when these exceptions occur and be led to believe something has gone wrong.  But everything’s doing what it should.  If you hit...

posted @ Wednesday, April 1, 2009 10:33 AM | Feedback (6)

Custom Controls Everywhere Saves the Day

I just cannot advocate Custom Controls Everywhere strongly enough.  I find benefits for it all the time.  It saved the day again today.  Self-validating controls played into this one too. Here's what a co-worker reported to me a few minutes ago: Ron Ellis says (1:45 PM): on the textbox format = decimal Ron Ellis says (1:46 PM): if you enter just a "-" it bombs To which, I replied, "Lovely.  It sounds like our...

posted @ Thursday, March 13, 2008 2:11 PM | Feedback (1)

Self-Validating Form Controls

Ever since I started working with ASP.NET, back in v1.0 Beta 1, I've been creating self-validating form controls.  I've gone through a few iterations with them, but overall, the concept has stayed the same. Here's a fun link to an archive of the old Cincinnati .NET user group.  It shows where I gave a presentation back in February of 2003 on this topic.  I was trying to remember when that was, and archive.org was able to remember for me. On my current project, our Textbox got the most attention for validation.  We also...

posted @ Tuesday, February 12, 2008 10:16 PM | Feedback (2)

MVC and MVP Validation

I started a thread on Hanselforum regarding MVC and MVP validation.  Scott responded and I've added another post.  We're discussing where validation should be done in the MVC framework.  Here's what I wrote up as my latest post: It seems like the Model should "own" validation, but for usability, the View must be able to apply it. Ultimately, I think validation needs to be applied at the time of entry as well as at the time of persistence. I don't like self-servicing models (that can persist themselves), and I like being able to validate models in different...

posted @ Tuesday, January 29, 2008 9:32 AM | Feedback (0)

Extended MVP Pattern - Article Recap

Over the past few months, I posted several times about my extended MVP Pattern.  Since this was an ill-planned series of posts, I wanted to post a list of the articles involved. Scott Hanselman's post showing off the ASP.NET MVC framework triggered me to finally write about what I had created. Here's the list of my articles though: An Extended MVP Pattern (MVP Validation) MVC/MVP Validation - How's it done? MVC vs. MVP Criticism of my Extended MVP Pattern Response to Criticism...

posted @ Wednesday, January 23, 2008 10:42 PM | Feedback (0)

ASP.NET MVC - First Use

I've been using the ASP.NET MVC extensions tonight; this is the first time I've played with it.  My initial reaction is a bit mixed. The first thing that I got irritated by was that I cannot use any of the ASP.NET validator controls.  I was creating a login form with email address and password, and I wanted to require both of the fields.  With MVC, I can't just slap a validator on and call it a day; I actually have to write the validation logic myself.  Oh, and I can't just use a ValidationSummary control either--I have to render...

posted @ Thursday, January 17, 2008 2:32 AM | Feedback (2)

Extended MVP Pattern - Domain Validation

When I wrote my Extended MVP Pattern article back in November, it got some criticism.  Specifically: Each 'thing' in the model should include it's own business rules, validation rules, security, and persistance [sic] rules. I argued that I've seen times with having all of the validation (and all other logic) in the domain layer doesn't pan out.  However, what I didn't talk about in detail was that my extended MVP pattern actually satisfies the desire to put the validation logic into the domain layer. When I started creating my pattern, having the validation...

posted @ Tuesday, January 15, 2008 1:20 AM | Feedback (16)

Response to Criticism of my Extended MVP Pattern

As I posted, I received some criticism about my extended MVP pattern over on ScottGu's blog.  I wanted to submit my response here, and I'll link to this post in a comment on ScottGu's blog. I will venture to guess that the vast majority of the folks that read ScottGu's blog are pretty sharp folks.  And experienced software developers know that there is no sure-fire solution to every problem; there is no single architecture or design pattern that fits every project; there is no silver bullet so to speak.  Microsoft's offering of an MVC framework as well as a...

posted @ Monday, November 19, 2007 10:02 PM | Feedback (3)

Criticism of my Extended MVP Pattern

Over on the ASP.NET MVC (Part 1) thread on ScottGu's blog, there's a little discussion between myself and a couple of others about whether or not a controller should have any logic in it. Doug Ramirez stated: Each 'thing' in the model should include it's own business rules, validation rules, security, and persistance [sic] rules. Doug was stating that Microsoft should publish best practices and guidelines for how to build MVC applications, and he was dictating that controllers should not have any logic--all logic should be housed within your business objects. I...

posted @ Monday, November 19, 2007 5:50 PM | Feedback (0)

MVC/MVP Validation - How's it done?

ScottGu posted Part 1 of a series about ASP.NET MVC.  One of the comments asked, "How is input validation going to work?"  That's something that I've been wondering about as well.  With the ASP.NET MVC framework, and others it seems, if the controller has no reference to the view, how does the controller tell the view that the first name field is required?  How does the controller tell the view that the phone number field must be a valid US phone number?  Moreover, how does the controller enforce field-level security, or anything else that would require fine-grained control over the...

posted @ Tuesday, November 13, 2007 12:25 PM | Feedback (0)

An Extended MVP Pattern (MVP Validation)

Until last year, I had never used an MVP/MVC pattern before.  I had read about the patterns at Martin Fowler's site, although the Model View Presenter pattern has since been retired.  I had also seen some posts and demos and talks where people were using MVC/MVP frameworks.  But I kept thinking there was something missing.  I couldn't put my finger on it for a long time, but I just had that gut feeling that something wasn't covered. Last year though, I was assigned to a new project, for the first time in a few years.  The new project was...

posted @ Friday, November 9, 2007 3:43 AM | Feedback (1)