To the developer - thank you

Nov 11, 2013 at 6:10 AM
Edited Nov 11, 2013 at 2:52 PM
Thank you so much for making this open source. your plugin is a life saver. I would like to contribute some things to help other people like a range validator.

here is my class. this is only tested in my project. feel free to configure, share or do whatever you want with this little code. I am not responsible if it causes errors. OK?
//using statements here

namespace Eksponent.Dewd.Fields.Validators
{
   public class Range : IValidator
   {
      private XElement validatorElement;
      private object validateElementTo;
      private string errorMessage = "";
      private string _CompareToField = "";
      public Range(XElement validatorElement)
      {
         this.validatorElement = validatorElement;
      }
      public Range(XElement validatorElement, object validateTo, string ComparetoField)
      {
         this.validatorElement = validatorElement;
         this.validateElementTo = validateTo;
         this._CompareToField = ComparetoField;
         
      }

      public virtual string ErrorText
      {
         get
         {
            if (errorMessage == "")
               return validatorElement.GetAttribute("errorText", "Compared fields are invalid.");
            else
               return errorMessage;
         }
      }


      public virtual bool IsValid(object value)
      {
         string type = validatorElement.GetAttribute("dataType");//date, int, double, time
         string compareOperator = validatorElement.GetAttribute("compareOperator");//lt, gt, lte, gte, ne, eq
         long realVal = 0;
         long realValCompareTo = 0;

         if (validateElementTo != null)
         {
            if (type == "date" || type == "int" || type == "double" || type == "time")
            {
               if (type == "date")
               {
                  try
                  {
                     DateTime dtRealVal = (DateTime)value;
                     DateTime dtRealValCompareTo = (DateTime)validateElementTo;
                     realVal = (long)(dtRealVal - new DateTime(1970, 1, 1)).TotalSeconds;
                     realValCompareTo = (long)(dtRealValCompareTo - new DateTime(1970, 1, 1)).TotalSeconds;
                  }
                  catch (Exception ex)
                  {
                     errorMessage = "Invalid Date format";
                     return false;
                  }
               }
               else if (type == "time")
               {
                  try
                  {
                     TimeSpan tsRealVal = (TimeSpan)value;
                     TimeSpan tsRealValCompareTo = (TimeSpan)validateElementTo;

                     realVal = (long)tsRealVal.TotalSeconds;
                     realValCompareTo = (long)tsRealValCompareTo.TotalSeconds;
                  }
                  catch (Exception ex)
                  {
                     errorMessage = "Invalid Time format";
                     return false;
                  }
               }
               else
               {
                  try
                  {
                     realVal = (long)value;
                     realValCompareTo = (long)validateElementTo;
                  }
                  catch (Exception ex)
                  {
                     errorMessage = "Invalid Numeric format";
                     return false;
                  }
               }

               if (compareOperator == "lt")
               {
                  if (realVal < realValCompareTo)
                  {
                     return true;
                  }
                  else {
                     errorMessage = "Field {0} should be less than " + _CompareToField;
                     return false;
                  }
               }
               else if (compareOperator == "lte")
               {
                  if (realVal <= realValCompareTo)
                  {
                     return true;
                  }
                  else
                  {
                     errorMessage = "Field {0} should be less than or equal to " + _CompareToField;
                     return false;
                  }
               }
               else if (compareOperator == "gt")
               {
                  if (realVal > realValCompareTo)
                  {
                     return true;
                  }
                  else
                  {
                     errorMessage = "Field {0} should be greater than " + _CompareToField;
                     return false;
                  }
               }
               else if (compareOperator == "gte")
               {
                  if (realVal >= realValCompareTo)
                  {
                     return true;
                  }
                  else
                  {
                     errorMessage = "Field {0} should be greater than or equal to " + _CompareToField;
                     return false;
                  }
               }
               else if (compareOperator == "ne")
               {
                  if (realVal != realValCompareTo)
                  {
                     return true;
                  }
                  else
                  {
                     errorMessage = "Field {0} should be not equal to " + _CompareToField;
                     return false;
                  }
               }
               else if (compareOperator == "eq")
               {
                  if (realVal == realValCompareTo)
                  {
                     return true;
                  }
                  else
                  {
                     errorMessage = "Field {0} should be equal to " + _CompareToField;
                     return false;
                  }
               }
               else
               {
                  errorMessage = "Operator setting on field {0} is missing or invalid.";
                  return false;
               }
            }
            else
            {
               errorMessage = "Data Type setting on field {0} is missing or invalid.";
               return false;
            }
         }
         else {
            errorMessage = "Control To Compare setting on field {0} is missing or invalid.";
            return false;
         }

         
      }
   }
}
And in Editor.cs
void BeforeSaveValidate(object sender, BeforeSaveEventArgs e)
        {
            var sb = new StringBuilder();
            sb.Append("<ul>");
            
            foreach (IField field in Fields)
            {
                if (!e.Values.ContainsKey(field.UniqueID))
                    continue; // not editor control field (so ignored in terms of validation)
                var value = e.Values[field.UniqueID];

                // loop over each validation node and do validation
                foreach (XElement valElement in field.Xml.Elements("validation"))
                {
                   
                    // get validator
                    var validator = TypeInstantiater.GetInstance<IValidator>(
                        valElement.GetAttribute("type"),
                        typeof(Required),
                        new string[] { "Eksponent.Dewd.Fields.Validators.{0},Eksponent.Dewd" },
                        valElement);

                    
                   if(validator.ToString().Contains("Range")){
                      
                      foreach (IField field2 in Fields) 
                      {
                         //valType += "," + field2.UniqueID;
                         if (field.Xml.GetAttribute("tabTitle") + "_" + valElement.GetAttribute("compareTo") == field2.UniqueID)
                         {
                            validator = new Range(valElement, e.Values[field2.UniqueID], valElement.GetAttribute("compareTo"));
                            break;
                         }
                      }
                   }
                    
                    if (!validator.IsValid(value))
                    {
                        // halt further validation on current field
                        e.Cancel = true;
                        var fieldTitle = field.Xml.GetAttribute("title");
                        var tabTitle = field.Xml.GetAttribute("tabTitle");
                        if (!String.IsNullOrEmpty(tabTitle))
                            fieldTitle = tabTitle + " - " + fieldTitle;
                        sb.AppendFormat("<li>" + validator.ErrorText + "</li>", fieldTitle);
                        
                        break; 
                    }
                   
                }

            }
            sb.Append("</ul>");
            if (e.Cancel)
                e.Result = new SaveResult() { Success = false, ErrorMessage = sb.ToString()};
        }
Coordinator
Nov 11, 2013 at 10:15 AM
Hi Denise,

Thank you very much, it's always great to hear from people, who find the plugin useful. And truly awesome of you to take the time to share your enhancements (few do), I greatly appreciate it :)

I'll see how I can integrate the range-validator in the new version 2.0 of Dewd. I've been hung up on a large client project for a while, but should get some time to do it in the coming weeks (hmm, well, realistically around the Christmas holidays) where I'll also be looking to upgrade it to the new version of Umbraco.

Thanks again!
  • Sune
Marked as answer by denise_delbando on 11/11/2013 at 7:09 AM
Nov 11, 2013 at 3:09 PM
I will try to contribute as much as I can. Keep up the good work.