Wednesday, January 20, 2010

Form validation using jQuery

If you are using jQuery, you can certainly get the jQuery Validation plugin to implement your client side validations. Plugin is available at jQuery Validation Plugin and it comes with Demo. Look at the source code of demo and I believe that is the only thing you need to know to use jQuery Validation.

Lack of proper validation model in MVC 1.0

Now I have been working with MVC 1.0 for quite some time and figuring out some short comings. One major short coming is that there is no proper model for implementing validations. Validation at server side and client side. There are n number of open source tools but none of them are so mature that you can promote them to production. Following are some tools that I evaluated:

MVC.Net Validation toolkit (http://mvcvalidatortoolkit.codeplex.com/) – This toolkit is good in terms of defining server side and client side validations. We just need to define all validations at server side and it automatically takes those validations to client side. However the advantage of this toolkit is disadvantage as well. Validations are defined outside the domain model (your entity). This approach of defining domain validations is not a very good approach and I would opt for it as a last resort.

Data Annotations – .Net Framework 3.5 there is a new functionality of Data Annotations (System.ComponentModel.DataAnnotations). Data annotations have very rich functionality where we can decorate each property of our domain entity. MVC.Net has good integrations with data annotation. If you make your MVC.Net view a strong typed view then MVC.Net framework starts understanding the data annotations of our domain entity. However this model is giving us only server side validation. xVal (http://xval.codeplex.com/) is one tool that can actually retrieve the domain validations from server and can populate those validations as client side validations. However in my evaluation I found that xVal is not able to retrieve all domain validations and whatever it retrieved were also not functional.

But all this is applicable only for MVC 2.0. MVC 1.0 does not understand data annotations and to get it working you need to download non production ready code from codeplex. Basically you need to get DataAnnotations model binder for MVC 1.0 and configure it. For more detail on this please visit ASP.Net MVC Validation.

Enterprise Library Validation Block – In order to achieve our purpose we don’t need to implement EL Validation Block. We can just use the assembly which gives us the functionality to decorate our domain entity with some validations. Use of this block gives us server side validation and then we need to use MVCValidator (http://www.codeplex.com/aspmvcvalidation) library which works with EL Validation block to implement client side validations. However MVCValidator library is also not so good for production as it does not provide support to validate value types. It works only with string data type.

Given all this I decided that I am not going to take any more pain of evaluating open source libraries. I am going to wait for MVC 2.0 where I get proper validation model inbuilt. Till then I will live only with client side validation by implementing jQuery validation plugin. To save myself from corrupt data I can implement proper constraint at database. This is temporary arrangement till MVC 2.0 comes out.

Wednesday, January 06, 2010

jQuery Hide/Close link

Phil Haack has written about how to close a “div” area using jQuery in his article here http://haacked.com/archive/2009/12/25/jquery-hide-close-link.aspx. However in his article he is demoing “slideUp()” function of jQuery which enables you to close the div. To show the same div again you will need to provide another link which will call “slideDown()”.

However there is another method “slideToggle()”. This method can toggle the visibility of div. If it is visible it will be hidden and if hidden it will be shown again. Following is demo of the functionality:

Welcome to ASP.NET!

To learn more about ASP.NET visit www.asp.net.

Following is the HTML markup for the same:

<div><a id="myLink" onclick="javascript:$('#myLink').html($('#myLink').html() == 'Show Info' ? 'Hide Info' : 'Show Info');$('#myDiv').slideToggle();" href="#">Hide Info</a></div>


<div id="myDiv">


  <h2>Welcome to ASP.NET! </h2>


  <p>To learn more about ASP.NET visit <a title="ASP.NET Website" href="http://www.asp.net">www.asp.net</a>. </p>


</div>


If you notice the onclick() of anchor tag, you will definitely frown on my inline javascript. But that is because I was not able to get Script tag working with Blogger and anyway I quickly wanted to show you slideToggle() functionality of jQuery.

Tuesday, January 05, 2010

SQLite

SQLite is a compact database engine. Here is the brief description about SQLite, “SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files. A complete SQL database with multiple tables, indices, triggers, and views, is contained in a single disk file. The database file format is cross-platform - you can freely copy a database between 32-bit and 64-bit systems or between big-endian and little-endian architectures.”

There is no need of any installation here. You just copy your database file along with rest of your application and you are off using all database capabilities. You have tables, views, triggers, transactions everything barring SP support. Lack of SP support might be an issue but do you really need SPs in small level applications where most of the things might be mere CRUD. You will not get these features if you are using flat/xml file or for that sake windows registry also. This makes this small database eligible for all your quick applications. It also supports most of SQL-92 standard, so if you grow big and need enterprise level server, you can move to SQL Server/Oracle any day. Here is the list of links that you will find helpful when working with SQLite.

  1. SQLite dll - http://www.sqlite.org/
  2. SQLite Tutorial - http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
  3. SQLite with .net - http://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins/
  4. SQLite .net data provider - http://sqlite.phxsoftware.com/
  5. SQLite Administrator - http://sqliteadmin.orbmu2k.de/ 

Good luck with SQLite.