Data Binding in Silverlight with RIA and Entity Framework – Part 3 (Validating Input Data)
Validation is a integral part of software development.In Silverlight validation approach has been changed or you can say evolved from version 2.So this post will highlight the options available for validation with continuation of StatesOfIndia application
- Data Binding in Silverlight with RIA and Entity Framework – Part 1 (Displaying Data)
- Data Binding in Silverlight with RIA and Entity Framework – Part 2 (Editing / Updating Data)
- Data Binding in Silverlight with RIA and Entity Framework – Part 3 (Validating Input Data)
Source Code and Demo Link
Hosted Application : Live Sample
Source Code : StatesOfIndia_WithValidation
Extending StatesOfIndia Application
In Continuation to the SOI application we will add validation to the user input , collectively i want to put validation for following logic ,
- State Name Mandatory and Length should not exceed more than 15
- Data type Validation for Population,Literacy,No Of District ,Area Which should allow Numeric Entry only
- Website URL Validation
- Define Population ,Literacy Range
- Cross Check State Name Weather it exist in Database (Cross Property Validation)
Validation In RIA With Silverlight
There are many ways of validating the User input either you can do it at Client side without any server round-trip or request or else Validation at server side or async validation . It depends on your choice and scenario .Prior version of Silverlight primarily dependend on Exception Based validation and with current available solution it does not make sense to discuss so I have skipped Exception Based Validation
Silverlight 4 exposes basically following methods of validation
- Using Metadata Attribute
- Code Behind Approach (Can be used at Client side using Async Operations or else Server side only)
With Attribute Metadata
As mentioned earlier the validation rule can be defined at entity level and their member.To do so make sure that you have selected the Metdata generation option while adding the DomainService.This option will add a metadata class “DomainService_SOI.metadata.cs” .Checking the class file we can find the the entity and its members.
Now suppose i want enforce stateName as Mandatory and the character length of of the state should not exceed 15 then
Now Build your application and check the generated code at client side silverlight project.
Propagated Client Side Validation Code
If you notice then you will find the attributes we added to the model metadata member is propagated to client side automatically.We can show custom error message by providing the Error Message Attribute as bellow.
Following are some of the other attributes for property level validation defined in System.ComponentModel.DataAnnotations namespace are as follows
- StringLengthAttribute and Others Check with detailed List
So as mentioned above the SOI application basic validations can be achieved with following attributes.
State Name Mandatory and Length should not exceed more than 15
As the validation need to be fired on textbox leave event , we need to do some ground clearance work although against wish ,Check with my last Blogpost.
Website URL Validation
Define Population ,Literacy Range
Data type Validation for Population,Literacy,No Of District ,Area Which should allow Numeric Entry only.As we need to restrict user while entering the value i have handled it in code behind at KeyDown event.
Custom attribute Validation
Although default attribute provides almost all standard way of validation sometimes custom validation are unavoidable.For e.g. here the wiki info content must be more than 3 words.To add custom validation keep following points in mind.
- Add a class which will contain the validation logic at server side
- make sure that your class will have a shared.cs extension so that the validation can be propagated to the client side
Lets do it , Add class StateValidations.shared.cs to the Server side project of the solution.
The custom validation check is a simple C# code named “ValidInfo” which is a static member and takes ValidationContext as argument other than the value need to be checked.ValidationContext represents state and service of the object to be validated.Once it the validation succeeded it returns a ValidationResult object.
Once you built the project ,check with the Silverlight client side project .The Generated Code stub will now have the same shared.cs .
To implement the custom validator lets move to our TasksDomainService.metadata.cs at server side project and add following attribute to the WikiInfo property.
When CustomValidation attribute applied to a property, the attribute is invoked whenever a value is assigned to that property. When it is applied to a method, the attribute is invoked whenever the program calls that method. When it is applied to a method parameter, the attribute is invoked before the method is called.A more detailed article can be found here .
Lets Run the application and check with the result.
Asynchronous Validation to check weather State Exist against database
In SOI app its pretty normal to check the State weather it exist in database before adding it to the collection.Instead of validating the it on button click it will be nice if same can be validated on the fly , once the user leaves the state Textbox.For the validation to succeed it need to hit back to the database collection and need to verify.
Ahhh its quite exciting .Lets checks ,
- Add a new Method/Operation to domain service class “DomainService_SOI.cs” , Named “IsStateExist”.As you can find in the bellow the method have a [INVOKE] attribute which let the operations run without deferred execution .More Detail can be found Here)
- Add a custom validator method as shared code , as we have done in the above “ValidInfo” case.
- Then add the CustomValidation attribute to the metadata code.
Lets Run the application and let the validation do its work
Hope this post gives you a fair idea about data validation .Keep Learning ..
- Performance tips for RIA Service with Silverlight 4
- Busy Indicator with DomainDataSource control
- Authentication against custom Table In Silverlight4 Using RIA Service
- Data Aggregation Using Presentation Model in RIA and Silverlight 4
- Performing CRUD on Relational Data (Multiple table) using RIA in SL4
- Converting XML data to CLR object using XmlSerializer
- Consuming External WebService from Silverlight Application
- Validating Textbox on Lost Focus in RIA Service,Silverlight4
- Data Binding in Silverlight with RIA and Entity Framework – Part 2 (Updating Data)
- Doesn’t your girlfriend deserves more time rather than Silverlight ??
About Manas Patnaik (51 posts)
My name is Manas Patnaik as my domain name suggests, and I design & write software. I live in Bangalore. I used to live in Berhampur, Orissa. My e-mail address is manas002002 AT gmail.com.
Well first, there’s a day job at Infosys , Bangalore. There, I work as Technology Analyst .Previously I worked at Relyon Softech Ltd as Software Engineer .Professionally , I am working from last 4 years . My strength as architect and developer is in developing fault-tolerant, highly scalable, high quality enterprise systems. Since 2005 I am developing and deploying on .Net platform, with occassional forays in LISP,J2EE.You can find my resume here.
In any remaining free time, I used to be at community sites for posting solution or else you will find me in kitchen .
If there’s any time left over, i share my experience here in this blog,watch movies or else play strategy games (Rise of Nation my favorite).