Provides a validation rule that checks if the value in a control is valid. ValidationRule is designed to be a simple as possible to reduce overhead in run-time. Because each validation rule can be attach to a control, there could be many instances of this class.
Inheritance: ICloneable
Exemplo n.º 1
0
 /// <summary>
 /// Compare two values.
 /// </summary>
 /// <param name="leftText"></param>
 /// <param name="rightText"></param>
 /// <param name="op"></param>
 /// <param name="vr"></param>
 /// <returns></returns>
 public static bool Compare(string leftText,
                            string rightText,
                            ValidationCompareOperator op,
                            ValidationRule vr)
 {
     if (false == vr.IsCaseSensitive && vr.DataType == ValidationDataType.String)
     {
         leftText  = leftText.ToLower();
         rightText = rightText.ToLower();
     }
     return(ValidationUtil.CompareValues(leftText, rightText, op, vr.DataType));
 }
        public void TestConstructedCorrectly()
        {
            //---------------Set up test pack-------------------
            ValidationRule testRule = new ValidationRule();

            //---------------Execute Test ----------------------

            //---------------Test Result -----------------------
            Assert.IsTrue(testRule.IsCaseSensitive);
            Assert.AreEqual(ValidationDataType.String,testRule.DataType);
            Assert.AreSame(String.Empty,testRule.InitialValue);
            Assert.IsTrue(testRule.IsValid);
            Assert.IsFalse(testRule.IsRequired);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Perform Custom Validation on specific control.
        /// </summary>
        private ValidationRule CustomValidate(IControlHabanero ctrl)
        {
            ValidationRule        returnRule = new ValidationRule();
            List <ValidationRule> vr         = _validationRules[ctrl];

            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null)
                {
                    CustomValidationEventArgs e = new CustomValidationEventArgs(ctrl.Text, obj);
                    obj.OnCustomValidationMethod(e);
                }
                returnRule = obj;
            });
            return(returnRule);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Perform RequiredField Validation on a specific control.
        /// </summary>
        private ValidationRule RequiredFieldValidate(IControlHabanero ctrl)
        {
            ValidationRule        returnRule = new ValidationRule();
            List <ValidationRule> vr         = _validationRules[ctrl];

            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null && obj.IsRequired)
                {
                    obj.IsValid = obj.IsValid && !ValidationRule.Compare(ctrl.Text, obj.InitialValue, ValidationCompareOperator.Equal, obj);
                }
                returnRule = obj;
            });


            return(returnRule);
        }
        public void TestCompareStrings()
        {
            //---------------Set up test pack-------------------
            const string firstString = "FirstString";
            const string sameAsFirstString = "FirstString";
            const string myString = "Hello World";
            ValidationRule testRule = new ValidationRule();
            testRule.Operator = ValidationCompareOperator.Equal;
            testRule.DataType = ValidationDataType.String;

            //---------Assert Preconditions----------------------
            Assert.IsTrue(String.Equals(firstString, sameAsFirstString));

            //---------------Execute Test ----------------------
            bool result = ValidationUtil.CompareValues(firstString, sameAsFirstString, testRule.Operator, testRule.DataType);
            bool falseResult = ValidationUtil.CompareValues(firstString, myString, testRule.Operator, testRule.DataType);

            //---------------Test Result -----------------------
            Assert.IsTrue(result);
            Assert.IsFalse(falseResult);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Set validation rule.
        /// </summary>
        public void SetValidationRule(IControlHabanero inputComponent, ValidationRule vr)
        {
            if (inputComponent != null)
            {
                // only throw error in DesignMode

                if (!this.CanExtend(inputComponent))
                {
                    throw new InvalidOperationException(inputComponent.GetType()
                                                        + " is not supported by the validation provider.");
                }

                if (!this.IsDefaultRange(vr) &&
                    ValidationRule.Compare(vr.MinimumValue, vr.MaximumValue, ValidationCompareOperator.GreaterThanEqual, vr))
                {
                    throw new ArgumentException("MinimumValue must not be greater than or equal to MaximumValue.");
                }


                //ValidationRule vrOld = this._ValidationRules[inputComponent] as ValidationRule;

                // if new rule is valid and in not DesignMode, clone rule
                if ((vr != null))
                {
                    vr = vr.Clone() as ValidationRule;
                }
                if (!this._validationRules.ContainsKey(inputComponent))
                {
                    List <ValidationRule> vrList = new List <ValidationRule>();
                    vrList.Add(vr);
                    this._validationRules.Add(inputComponent, vrList);
                }
                else if ((vr != null) && !this._validationRules[inputComponent].Contains(vr))
                {
                    this._validationRules[inputComponent].Add(vr);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Perform CompareValidate on a specific control.
        /// </summary>
        /// <returns>true if control has no validation rule.</returns>
        private ValidationRule CompareValidate(IControlHabanero ctrl)
        {
            ValidationRule        returnRule = new ValidationRule();
            List <ValidationRule> vr         = _validationRules[ctrl];

            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null)
                {
                    if (this._defaultValidationRule.ValueToCompare.Equals(obj.ValueToCompare) &&
                        this._defaultValidationRule.Operator.Equals(obj.Operator))
                    {
                        returnRule = obj;
                    }
                    else
                    {
                        obj.IsValid = ValidationRule.Compare(ctrl.Text, obj.ValueToCompare, obj.Operator, obj);
                    }
                }
            });


            return(returnRule);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Check if validation rule range is default.
 /// </summary>
 /// <param name="vr"></param>
 /// <returns></returns>
 private bool IsDefaultRange(ValidationRule vr)
 {
     return(this._defaultValidationRule.MinimumValue.Equals(vr.MinimumValue) &&
            this._defaultValidationRule.MaximumValue.Equals(vr.MaximumValue));
 }
		///<summary>
		/// Constructs the <see cref="CustomValidationEventArgs"/>
		///</summary>
		///<param name="Value"></param>
		///<param name="vr"></param>
		public CustomValidationEventArgs(object Value, ValidationRule vr)
		{
			this._value = Value;
			this._validationRule = vr;
		}
Exemplo n.º 10
0
		/// <summary>
		/// Perform CompareValidate on a specific control.
		/// </summary>
		/// <returns>true if control has no validation rule.</returns>
        private ValidationRule CompareValidate(IControlHabanero ctrl)
		{
            ValidationRule returnRule = new ValidationRule();
            List<ValidationRule> vr = _validationRules[ctrl];
            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null)
                {
                    if (this._defaultValidationRule.ValueToCompare.Equals(obj.ValueToCompare)
                        && this._defaultValidationRule.Operator.Equals(obj.Operator))
                    {
                        returnRule = obj;
                    }
                    else
                    {
                        obj.IsValid = ValidationRule.Compare(ctrl.Text, obj.ValueToCompare, obj.Operator, obj);
                    }
                }
            });


            return returnRule;
		}
Exemplo n.º 11
0
		/// <summary>
		/// Perform Regular Expression Validation on a specific control.
		/// </summary>
        private ValidationRule RegularExpressionValidate(IControlHabanero ctrl)
		{
            List<ValidationRule> vr = _validationRules[ctrl];
            ValidationRule returnRule = new ValidationRule();

            if (vr != null)
            {
                vr.ForEach(delegate(ValidationRule obj)
                {
                    try
                    {
                        if (this._defaultValidationRule.RegExPattern.Equals(obj.RegExPattern)) returnRule= obj;

                        obj.IsValid = ValidationUtil.ValidateRegEx(ctrl.Text, obj.RegExPattern);
                        returnRule = obj;
                    }
                    catch (Exception ex)
                    {
                        obj.ResultErrorMessage = "RegEx Validation Exception: " + ex.Message + Environment.NewLine;
                        obj.IsValid = false;
                    }

                });
                
            }
            return returnRule;
		}
Exemplo n.º 12
0
		/// <summary>
		/// Perform RequiredField Validation on a specific control.
		/// </summary>
        private ValidationRule RequiredFieldValidate(IControlHabanero ctrl)
		{
            ValidationRule returnRule = new ValidationRule();
            List<ValidationRule> vr = _validationRules[ctrl];
            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null && obj.IsRequired)
                {
                    obj.IsValid = obj.IsValid && !ValidationRule.Compare(ctrl.Text, obj.InitialValue, ValidationCompareOperator.Equal, obj);
                }
                returnRule = obj;
            });


            return returnRule;
		}
Exemplo n.º 13
0
		/// <summary>
		/// Check if validation rule range is default.
		/// </summary>
		/// <param name="vr"></param>
		/// <returns></returns>
		private bool IsDefaultRange(ValidationRule vr)
		{
			return (this._defaultValidationRule.MinimumValue.Equals(vr.MinimumValue)
				&& this._defaultValidationRule.MaximumValue.Equals(vr.MaximumValue));
			
		}
Exemplo n.º 14
0
		/// <summary>
		/// Perform Range Validation on a specific control.
		/// </summary>
        private ValidationRule RangeValidate(IControlHabanero ctrl)
		{
            List<ValidationRule> vr = _validationRules[ctrl];
            ValidationRule returnRule = new ValidationRule();
            if (vr != null)
            {
                vr.ForEach(delegate(ValidationRule obj)
                {
                    if (this.IsDefaultRange(obj))
                    {
                        returnRule = obj;
                    }
                    else
                    {
                        obj.IsValid =ValidationRule.Compare(ctrl.Text, obj.MinimumValue,ValidationCompareOperator.GreaterThanEqual, obj);

                        if (obj.IsValid)
                            obj.IsValid = ValidationRule.Compare(ctrl.Text, obj.MaximumValue,ValidationCompareOperator.LessThanEqual, obj);
                        returnRule = obj;
                    }
                });
                
            }
            return returnRule;
		}
Exemplo n.º 15
0
		/// <summary>
		/// Perform Custom Validation on specific control.
		/// </summary>
        private ValidationRule CustomValidate(IControlHabanero ctrl)
		{
            ValidationRule returnRule = new ValidationRule();
            List<ValidationRule> vr = _validationRules[ctrl];
            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null)
                {
                    CustomValidationEventArgs e = new CustomValidationEventArgs(ctrl.Text, obj);
                    obj.OnCustomValidationMethod(e);
                }
                returnRule = obj;
            });
		    return returnRule;
		}
Exemplo n.º 16
0
        public void TestIsRequiredValdiation()
        {
            //---------------Set up test pack-------------------
            const string myString = "Hello";
            ValidationRule testRule = new ValidationRule();
            testRule.IsRequired = true;

            //---------------Execute Test ----------------------
            bool result = ValidationUtil.CompareValues(myString, "", testRule.Operator, testRule.DataType);

            //---------------Test Result -----------------------
            Assert.IsTrue(result);

        }
Exemplo n.º 17
0
 ///<summary>
 /// Constructs the <see cref="CustomValidationEventArgs"/>
 ///</summary>
 ///<param name="Value"></param>
 ///<param name="vr"></param>
 public CustomValidationEventArgs(object Value, ValidationRule vr)
 {
     this._value          = Value;
     this._validationRule = vr;
 }
Exemplo n.º 18
0
		/// <summary>
		/// Set validation rule.
		/// </summary>
        public void SetValidationRule(IControlHabanero inputComponent, ValidationRule vr)
		{
			if (inputComponent != null)
			{
				// only throw error in DesignMode
				
					if (!this.CanExtend(inputComponent))
						throw new InvalidOperationException(inputComponent.GetType() 
							+ " is not supported by the validation provider.");

					if (!this.IsDefaultRange(vr) 
						&& ValidationRule.Compare(vr.MinimumValue, vr.MaximumValue, ValidationCompareOperator.GreaterThanEqual, vr))
						throw new ArgumentException("MinimumValue must not be greater than or equal to MaximumValue.");
				

				//ValidationRule vrOld = this._ValidationRules[inputComponent] as ValidationRule;

				// if new rule is valid and in not DesignMode, clone rule
				if ((vr != null) )
				{
					vr = vr.Clone() as ValidationRule;
				}
 			    if(!this._validationRules.ContainsKey(inputComponent))
 			    {
 			        List<ValidationRule> vrList = new List<ValidationRule>();
                    vrList.Add(vr);
                    this._validationRules.Add(inputComponent,vrList);
 			    }
				else if ((vr != null) && !this._validationRules[inputComponent].Contains(vr))
				{
                    
					this._validationRules[inputComponent].Add(vr);
				}

			}
		}
Exemplo n.º 19
0
		/// <summary>
		/// Validate Data Type.
		/// </summary>
        private ValidationRule DataTypeValidate(IControlHabanero ctrl)
		{
            ValidationRule returnRule = new ValidationRule();
            List<ValidationRule> vr = this._validationRules[ctrl];
            vr.ForEach(delegate(ValidationRule obj)
            {
                if (obj != null && obj.Operator.Equals(ValidationCompareOperator.DataTypeCheck))
                {
                    if (obj.DataType.Equals(this._defaultValidationRule.DataType))
                    {
                        returnRule = obj;
                    }
                    else
                    {
                        System.Web.UI.WebControls.ValidationDataType vdt =
                            (System.Web.UI.WebControls.ValidationDataType) Enum.Parse(typeof (System.Web.UI.WebControls.
                                                                               ValidationDataType),obj.DataType.ToString());

                        obj.IsValid = ValidationUtil.CanConvert(ctrl.Text, vdt);
                        returnRule = obj;
                    }
                }
            });
		    return returnRule;
		}
Exemplo n.º 20
0
 public void TestCompareIntegers()
 {
     //---------------Set up test pack-------------------
     const int firstInt = 10;
     const int sameAsFirstInt = 10;
     const int myInt = 80;
     ValidationRule testRule = new ValidationRule();
     testRule.Operator = ValidationCompareOperator.Equal;
     testRule.DataType = ValidationDataType.Integer;
     //-------------Test Preconditions------------------
     Assert.AreEqual(firstInt,sameAsFirstInt);
     //---------------Execute Test ----------------------
     bool result = ValidationUtil.CompareValues(Convert.ToString(firstInt), Convert.ToString(sameAsFirstInt), testRule.Operator, testRule.DataType);
     bool falseResult = ValidationUtil.CompareValues(Convert.ToString(firstInt), Convert.ToString(myInt), testRule.Operator, testRule.DataType);
     //---------------Test Result -----------------------
     Assert.IsTrue(result);
     Assert.IsFalse(falseResult);
 }
Exemplo n.º 21
0
		/// <summary>
		/// Compare two values.
		/// </summary>
		/// <param name="leftText"></param>
		/// <param name="rightText"></param>
		/// <param name="op"></param>
		/// <param name="vr"></param>
		/// <returns></returns>
		public static bool Compare(	string leftText, 
									string rightText, 
									ValidationCompareOperator op, 
									ValidationRule vr)
		{
			if (false == vr.IsCaseSensitive && vr.DataType == ValidationDataType.String)
			{
				leftText = leftText.ToLower();
				rightText = rightText.ToLower();
			}
			return ValidationUtil.CompareValues(leftText, rightText, op, vr.DataType);
		}