public override string GetErrorMessage(ValidationContext validationContext) {
   return LocalizedMessage.Format(validationContext.Property.DisplayName, validationContext.PropertyValue);
 }
    public async Task ChangeMessageBaseAndAssembly() {
      var em1 = await TestFns.NewEm(_serviceName);

      var emp = new Employee();
      var vr = new RequiredValidator().WithMessage("Model.NorthwindIB.CustomMessages2", typeof (Employee).Assembly);
      var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName");
      var vc = new ValidationContext(emp, dp, null);
      var ve = vr.Validate(vc);
      Assert.IsTrue(ve != null);
      Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("required") &&
                    ve.Message.Contains("CUSTOM 2"));
      Assert.IsTrue(ve.Context.Entity == emp);
      Assert.IsTrue(ve.Validator == vr);
      Assert.IsTrue(ve.Key != null);
    }
 protected override bool ValidateCore(ValidationContext context) {
   var value = (String) context.PropertyValue;
   if (value == null) return true;
   return value.StartsWith(CountryAbbrev);
 }
 public override string GetErrorMessage(ValidationContext validationContext)
 {
     return LocalizedMessage.ToString();
 }
    public async Task ChangeMessageString() {
      var em1 = await TestFns.NewEm(_serviceName);

      var emp = new Employee();
      var vr = new RequiredValidator().WithMessage("{0} is bad");
      var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName");
      var vc = new ValidationContext(emp, dp, null);
      var ve = vr.Validate(vc);
      Assert.IsTrue(ve != null);
      Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("bad"));
      Assert.IsTrue(ve.Context.Entity == emp);
      Assert.IsTrue(ve.Validator == vr);
      Assert.IsTrue(ve.Key != null);
    }
 protected override bool ValidateCore(ValidationContext context)
 {
     // This validator only validates US Zip Codes.
     var entity = context.Entity;
     if (entity.GetPropValue<string>("Country") == "USA")
     {
         var postalCode = entity.GetPropValue<string>("PostalCode");
         context.PropertyValue = postalCode;
         return IsValidZipCode(postalCode);
     }
     return true;
 }
 protected override bool ValidateCore(ValidationContext context)
 {
     return false;
 }
 protected override bool ValidateCore(ValidationContext context)
 {
     var value = (int) context.PropertyValue;
     return value != 0;
 }
 protected override bool ValidateCore(ValidationContext context)
 {
     var value = (String) context.PropertyValue;
     if (value == null) return true; // '== null' matches null and empty string
     return value.ToUpper().StartsWith("US");
 }
        public async Task CustomizeMessageString()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var customer = new Customer();
            var vr = new RequiredValidator().WithMessage("Dude! The {0} is really required ... seriously ... as in mandatory");

            var companyNameProp = customer.EntityAspect.EntityType.GetDataProperty("CompanyName");
            var context = new ValidationContext(customer, companyNameProp, null);
            var error = vr.Validate(context);
            Assert.IsTrue(error.Message.Contains("CompanyName") && error.Message.Contains("Dude"), "should be an error containing 'Dude'");
        }
 public ValidationContext(ValidationContext vc) {
   Entity = vc.Entity;
   Property = vc.Property;
   PropertyValue = vc.PropertyValue;
   ComplexObject = vc.ComplexObject;
 }
示例#12
0
    public EntityError Resolve(EntityManager em) {
      IsServerError = true;
      try {
        EntityType entityType = null;
        if (EntityTypeName != null) {
          var stName = TypeNameInfo.FromClrTypeName(EntityTypeName).ToClient(em.MetadataStore).StructuralTypeName;
          entityType = em.MetadataStore.GetEntityType(stName);
          var ek = new EntityKey(entityType, KeyValues);
          Entity = em.GetEntityByKey(ek);
        }

        
        if (entityType != null) {
          if (PropertyName != null) {
            Property = entityType.Properties.FirstOrDefault(p => p.NameOnServer == PropertyName);
            if (Property != null) {
              PropertyName = Property.Name;
            }
          }
          
          var vc = new ValidationContext(this.Entity);
          vc.Property = Property;
          var veKey = (ErrorName ?? ErrorMessage) + (PropertyName ?? "");
          var ve = new ValidationError(null, vc, ErrorMessage, veKey);
          ve.IsServerError = true;
          this.Entity.EntityAspect.ValidationErrors.Add(ve);
        }
      } catch (Exception e) {
        ErrorMessage = ( ErrorMessage ?? "") + ":  Unable to Resolve this error: " + e.Message;
      }
      return this;
    }