I have a strange problem with custom validation. I added two custom validation annotations to a property in an entity. For whatever reason when I run the app the validations do not work. 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. 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? Here is a sample of my
custom validation for both. 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?
Thanks in advance ,
shape87
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 = "Please enter a number between 1 and " + categoriesRepo.Categories.Count();
}
public override bool IsValid(object value)
{
int sortorder = (int)value;
if (sortorder < 1 || sortorder > 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 = "Please enter a unique value for each sort order";
}
public List<int> list = new List<int>();
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="Please fill in a value for SortOrder!")]
[SortOrder]
[UniqueValue]
public int SortOrder { get; set; }
public int CategoriesID { get; set; }
public int CompanyID { get; set; }
public virtual Categories Categories { get; set; }
}
Thanks for taking a look Young! I tried to see if each of the custom validations would work by themselves and they did. I think I found the culprit, both validations are reading data from the same repository at the same time, thus canceling eachother out.
I', suprised it never threw an open data reader exception. 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. 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?
shape87
Member
16 Points
17 Posts
Custom Validation Quirks
Feb 19, 2012 10:07 PM|LINK
Hello All,
I have a strange problem with custom validation. I added two custom validation annotations to a property in an entity. For whatever reason when I run the app the validations do not work. 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. 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? Here is a sample of my custom validation for both. 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?
Thanks in advance
,
shape87
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 = "Please enter a number between 1 and " + categoriesRepo.Categories.Count(); } public override bool IsValid(object value) { int sortorder = (int)value; if (sortorder < 1 || sortorder > 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 = "Please enter a unique value for each sort order"; } public List<int> list = new List<int>(); 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="Please fill in a value for SortOrder!")] [SortOrder] [UniqueValue] public int SortOrder { get; set; } public int CategoriesID { get; set; } public int CompanyID { get; set; } public virtual Categories Categories { get; set; } }Young Yang -...
All-Star
21742 Points
1825 Posts
Microsoft
Re: Custom Validation Quirks
Feb 21, 2012 07:09 AM|LINK
Hi
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?
I prefer to keep the validation in the domain project.
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
shape87
Member
16 Points
17 Posts
Re: Custom Validation Quirks
Feb 21, 2012 05:18 PM|LINK
Thanks for taking a look Young! I tried to see if each of the custom validations would work by themselves and they did. I think I found the culprit, both validations are reading data from the same repository at the same time, thus canceling eachother out. I', suprised it never threw an open data reader exception. 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. 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?
Thanks Again,
shape87