Custom Validation Quirkshttp://forums.asp.net/t/1771324.aspx/1?Custom+Validation+QuirksTue, 21 Feb 2012 17:18:51 -050017713244839977http://forums.asp.net/p/1771324/4839977.aspx/1?Custom+Validation+QuirksCustom Validation Quirks <p>Hello All,</p> <p>I have a strange problem with custom validation. &nbsp;I added two custom validation annotations to a property in an entity. &nbsp;For whatever reason when I run the app the validations do not work. &nbsp;If I remove the validation build and then refresh the program, then re-add the validation, build and refresh the validation works just like I expect it to. &nbsp;Obviously that is not going to be sufficient, is ther a reason the custom validation will not work right away and without this dreadful process? &nbsp;Here is a sample of my custom validation for both. &nbsp;One last question, if I had multiple projects on my solution would it be more conventional to keep the validation in the domain project or the WebUI?</p> <p>Thanks in advance&nbsp;<img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0">,</p> <p>shape87</p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; using InterfaceDraft3.Domain.Abstract; using InterfaceDraft3.Domain.Concrete; namespace InterfaceDraft3.Domain.Validation { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)] public class SortOrder : ValidationAttribute { private ICustomMenuRepository menCustRepo = new EFCustomMenuRepository(); private ICategoriesRepository categoriesRepo = new EFCategoriesRepository(); public SortOrder() { ErrorMessage = &quot;Please enter a number between 1 and &quot; &#43; categoriesRepo.Categories.Count(); } public override bool IsValid(object value) { int sortorder = (int)value; if (sortorder &lt; 1 || sortorder &gt; categoriesRepo.Categories.Count()) { return false; } else { return true; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; using InterfaceDraft3.Domain.Abstract; using InterfaceDraft3.Domain.Concrete; namespace InterfaceDraft3.Domain.Validation { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = true)] public class UniqueValue : ValidationAttribute { private ICustomMenuRepository menCustRepo = new EFCustomMenuRepository(); private ICategoriesRepository categoriesRepo = new EFCategoriesRepository(); public UniqueValue() { ErrorMessage = &quot;Please enter a unique value for each sort order&quot;; } public List&lt;int&gt; list = new List&lt;int&gt;(); public override bool IsValid(object value) { int sortorder = (int)value; if (list.Contains(sortorder)) { list.Clear(); return false; } else { list.Add(sortorder); if (list.Count() == categoriesRepo.Categories.Count()) { list.Clear(); } return true; } } } } namespace InterfaceDraft3.Domain.Entities { public class CustomMenu { public int CustomMenuID { get; set; } public String OriginalName { get; set; } public String Name { get; set; } public bool Selected { get; set; } [Required(ErrorMessage=&quot;Please fill in a value for SortOrder!&quot;)] [SortOrder] [UniqueValue] public int SortOrder { get; set; } public int CategoriesID { get; set; } public int CompanyID { get; set; } public virtual Categories Categories { get; set; } }</pre> <p></p> 2012-02-19T22:07:52-05:004842384http://forums.asp.net/p/1771324/4842384.aspx/1?Re+Custom+Validation+QuirksRe: Custom Validation Quirks <p></p> <blockquote><span class="icon-blockquote"></span> <h4>shape87</h4> Obviously that is not going to be sufficient, is ther a reason the custom validation will not work right away and without this dreadful process?</blockquote> <p></p> <p>Hi</p> <p>According to your code, I couldn't found any incorrect. Maybe you can provide more details to us. And then the validation works well now, right?</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>shape87</h4> if I had multiple projects on my solution would it be more conventional to keep the validation in the domain project or the WebUI?</blockquote> <p></p> <p>I prefer to keep the validation in the domain project.</p> <p>Hope this helpful<br> Regards<br> Young Yang</p> 2012-02-21T07:09:08-05:004843593http://forums.asp.net/p/1771324/4843593.aspx/1?Re+Custom+Validation+QuirksRe: Custom Validation Quirks <p>Thanks for taking a look Young! &nbsp;I tried to see if each of the custom validations would work by themselves and they did. &nbsp;I think I found the culprit, both validations are reading data from the same repository at the same time, thus canceling eachother out. &nbsp;I', suprised it never threw an open data reader exception. &nbsp;What I did was I took out the categoriesRepo.Categories.Count() in one of the Custom Validtaions and replaced it with a static number and they both worked. &nbsp;Do you have a suggestion how to capture how many records a category repository has without having to call categoriesRepo.Categories.Count() like I do now?</p> <p>Thanks Again,</p> <p>shape87</p> 2012-02-21T17:18:51-05:00