<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Iteration #3 – Add form validation</title><link>http://www.asp.net</link><pubDate>Wed, 12 Oct 2011 08:11:31 GMT</pubDate><generator>umbraco</generator><description>Comments for Iteration #3 – Add form validation</description><language>en</language><atom:link href="http://www.asp.net/rss/comments/27615" rel="self" type="application/rss+xml" /><item><title>Comment Posted by jonnylynchy</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Sat, 07 Aug 2010 01:19:58 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-000000009784</guid><description><![CDATA[ <p>I&#39;ve followed this tutorial from iteration #1 with no issues until the Validation. Using your example with MVC 2, I get the following exception when attempting to pass empty values:</p><p></p><p>Object reference not set to an instance of an object.</p><p></p><p></p><p>Line 14:             if (contactToValidate.FirstName.Trim().Length == 0)</p><p></p><p>When I try to set a break point on the validation in the controller, it breaks in the model designer class before reaching the breakpoint:</p><p></p><p>Constraint Exception was unhandled by user code</p><p></p><p>Line 177 of ContactManagerModel.designer.cs --- _FirstName = StructuralObject.SetValidValue(value, false);</p><p></p><p>Did I miss something when setting up my data model?</p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/jonnylynchy.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by jamieBenzy</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Thu, 12 Aug 2010 13:48:26 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-000000009839</guid><description><![CDATA[ <p>I received the same null reference exception. Here&#39;s what I did to correct it.</p><p>1. changed the inherits overload in the page to: Inherits=&quot;System.Web.Mvc.ViewPage&lt;ContactManager.Models.Contact&gt;</p><p>2. changed the return view() overload to - return View(contactToEdit); in ContactController.Edit(Contact contactToEdit)</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/jamieBenzy.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by kaellinn</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Thu, 19 Aug 2010 17:19:16 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-000000009917</guid><description><![CDATA[ <p>jonnylynchy and jamieBenzy:</p><p></p><p>I ran into this problem also. In order to fix it, I added some boolean short-circuiting to my conditions. For instance, my condition for the FirstName field is now:</p><p></p><p>if (contactToValidate.FirstName == null || contactToValidate.FirstName.Trim().Length == 0)</p><p></p><p>If contactToValidate.FirstName is null, C# is smart enough to not even test the second half of the OR statement, so it doesn&#39;t generate the error. I added similar tests for the other fields also. Hopefully this helps!</p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/kaellinn.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by thebigbull</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Tue, 16 Nov 2010 12:37:39 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000010965</guid><description><![CDATA[ <p>I cannot believe you did not use string.IsNullOrEmpty(&quot;&quot;) function to validate strings. Microsoft provides these useful and fancy features but forgets to educate its own developers. that does not seem right.</p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/thebigbull.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by prolingua.geo</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Thu, 10 Mar 2011 08:25:42 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000012424</guid><description><![CDATA[ <p>change the code inside public ActionResult Create([Bind(Exclude = &quot;Id&quot;)] Contact contactToCreate)  to the following and rebuild your solution and try again.</p><p></p><p> if (!ModelState.IsValid)</p><p>            {</p><p>                if (string.IsNullOrEmpty(contactToCreate.FirstName))</p><p>                    ModelState.AddModelError(&quot;FirstName&quot;, &quot;First name is required.&quot;);</p><p>                if (string.IsNullOrEmpty(contactToCreate.LastName))</p><p>                    ModelState.AddModelError(&quot;LastName&quot;, &quot;Last name is required.&quot;);</p><p>                if (string.IsNullOrEmpty(contactToCreate.Phone))</p><p>                    ModelState.AddModelError(&quot;Phone&quot;, &quot;Invalid phone number.&quot;);</p><p>                else if (contactToCreate.Phone.Length &gt; 0 &amp;&amp; !Regex.IsMatch(contactToCreate.Phone, @&quot;((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}&quot;))</p><p>                    ModelState.AddModelError(&quot;Phone&quot;, &quot;Invalid phone number.&quot;);</p><p></p><p>                if (string.IsNullOrEmpty(contactToCreate.Email))</p><p>                    ModelState.AddModelError(&quot;Email&quot;, &quot;Invalid email address.&quot;);</p><p>                else if (contactToCreate.Email.Length &gt; 0 &amp;&amp; !Regex.IsMatch(contactToCreate.Email, @&quot;^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$&quot;))</p><p>                    ModelState.AddModelError(&quot;Email&quot;, &quot;Invalid email address.&quot;);</p><p>                return View(contactToCreate);</p><p>            }</p><p>            else</p><p>            {</p><p>                if (contactToCreate.FirstName.Trim().Length == 0)</p><p>                    ModelState.AddModelError(&quot;FirstName&quot;, &quot;First name is required.&quot;);</p><p>                if (contactToCreate.LastName.Trim().Length == 0)</p><p>                    ModelState.AddModelError(&quot;LastName&quot;, &quot;Last name is required.&quot;);</p><p>                if (contactToCreate.Phone.Length &gt; 0 &amp;&amp; !Regex.IsMatch(contactToCreate.Phone, @&quot;((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}&quot;))</p><p>                    ModelState.AddModelError(&quot;Phone&quot;, &quot;Invalid phone number.&quot;);</p><p>                if (contactToCreate.Email.Length &gt; 0 &amp;&amp; !Regex.IsMatch(contactToCreate.Email, @&quot;^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$&quot;))</p><p>                    ModelState.AddModelError(&quot;Email&quot;, &quot;Invalid email address.&quot;);</p><p>                if (!ModelState.IsValid) </p><p>                    return View(contactToCreate);</p><p>            }</p><p></p><p>            try</p><p>            {</p><p>                _entities.AddToContacts(contactToCreate);</p><p>                _entities.SaveChanges();</p><p>                return RedirectToAction(&quot;Index&quot;);</p><p>            }</p><p>            catch</p><p>            {</p><p>                return View(contactToCreate);</p><p>            }</p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/prolingua.geo.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by Tim.Yamkuza</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Wed, 18 May 2011 16:01:19 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000013212</guid><description><![CDATA[ <p>What order is the validation done in?  I&#39;ve tried several ideas, including string.IsNullOrEmpty and I always get an unhandled exception in the designer for my model (ContactManagerModel.Designer.cs) as if it validates against the database first and then runs the webpage code...</p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/Tim.Yamkuza.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by basbak</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Tue, 06 Sep 2011 10:25:12 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000014129</guid><description><![CDATA[ <p>Hello,</p><p></p><p>I am doing this app with Razor syntax in MVC3, it includes a class for the Contacts using DataAnnotations, yet I can&#39;t seem to get the client side validation to fire up.</p><p></p><p>Any suggestions?</p><p></p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/basbak.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by admiringworm</title><link>http://www.asp.net/mvc/tutorials/older-versions/contact-manager/iteration-3-add-form-validation-cs</link><pubDate>Wed, 12 Oct 2011 08:11:31 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000014406</guid><description><![CDATA[ <p>you need to refer to two scripts to get the DataAnnotations to work correctly in MVC3</p><p>you need jquery.validate.min.js and jquery.unobtrusive.min.js (both should be already in the &quot;Scripts&quot; directory. since both of them depends on jquery you would have to refer to it as well. (it should already be in _Layout.cshtml)</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/admiringworm.jpg?forceidenticon=false&amp;dt=635072386200000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item></channel></rss>