I use validation on Silverlight and I have some troubles: 1. I need checking filling the field and its uniqueness. To do it I added attributes to entity metadata:
[Required(ErrorMessage="Необходимо указать название производителя")]
[CustomValidation(typeof(MakerValidator), "CheckName")]
Wrote appropriate validators and function to check uniqueness:
[Invoke]
public bool IpAlreadyExist(string Ip, int ID)
{
if (ID > 0) return this.ObjectContext.OFFICE_IP.Any(a => a.IP == Ip && a.ID != ID && a.L_DEL == false);
return this.ObjectContext.OFFICE_IP.Any(a => a.IP == Ip && a.L_DEL == false);
}
(in case editing entity we check additionally existense another ID like checking.)
But we have some problems: 1 - when we create new entity and save it before input validation not work because did not Property changed 2 - very seldom after save validate field highlighted like inkorrect and if we delete and input the same value again, field will be correct. (I think that point is some propertyChanged is occurs before saving and Id it element 0, but this record exist in data base).
What can we do with this troubles?
Here's a piece of code analysis
public class OfficeIpValidator
{
public static ValidationResult CheckIp(string Ip, ValidationContext validationContext)
{
OFFICE_IP officeIp = (OFFICE_IP)validationContext.ObjectInstance;
if !SILVERLIGHT
IxoraDomainService service = new IxoraDomainService();
if (service.IpAlreadyExist(Ip, officeIp.ID))
{
return new ValidationResult("Указанный Ip адрес уже существует", new[] { validationContext.MemberName });
}
else
That is, the ID is requested from the previous state of the object. Therefore, at some point in time it may not be equal to zero, whereas the previous condition is zero. Can I get out from the validationContext the current state, rather than the previous state of the object?