RegexValidator in EntLib Validation not working for int or float values

Topics: Validation Application Block
Jun 7, 2010 at 7:07 AM
Hi, I am binding the RegexValidator in my Business Entitly class with a Float type property, where it is showing error that it expects the "String" value as input. How can I map it with float or integer type proprties. I am using EntLib version 4.1. Here is my code snippet: [RegexValidator(@"^[\-+]?[0-9]{1,4}(\.[0-9]{1,2})?$", MessageTemplate= "The value in field Internal Temp1 is not valid.Please enter the temperature value between 9999.99 to -9999.99")] public float InternalTemp2 { get { return m_InternalTemp2; } set { m_InternalTemp2 = value; } }
Jun 7, 2010 at 7:40 AM
Edited Jun 7, 2010 at 7:45 AM

Since RegexValidator is only for strings, the fastest workaround would be to create a public property which exposes the float type of property you want to validate as a string and associate it with the RegexValidator rather than with the float property.  Another workaround would be to create custom RegexValidators for both float and integer types.  Both simply make use of the existing RegexValidator inside.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Jun 7, 2010 at 10:30 AM
Thanks Sarah for quick reply. Can you send me example for custom RegexValidators. I have already short of time, as trying to implement it from last 2 days.
Jun 9, 2010 at 6:12 AM

Here's a sample: 

[ConfigurationElementType(typeof(CustomValidatorData))]
public class MyValidator :  Validator
{
        string pattern = null;
        RegexOptions option =  RegexOptions.None;
        bool negated = false;

        public MyValidator(NameValueCollection attributes)
            : base("messageTemplate", string.Empty)
        {
            pattern = attributes.Get("pattern");
            string regExOption = attributes.Get("RegExOption");
            if (!string.IsNullOrEmpty(regExOption))
            {
                option = (RegexOptions)Enum.Parse(typeof(RegexOptions), regExOption);
            }
            negated = attributes.Get("negated") != null ? bool.Parse(attributes.Get("negated")) : false;
        }


        protected override string DefaultMessageTemplate
        {
            get { return "your default message"; }
        }

        public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            bool logError = false;
            bool isObjectToValidateNull = objectToValidate == null;

            if (!isObjectToValidateNull)
            {
                Regex regex = new Regex(pattern, option);
                logError = !regex.IsMatch(objectToValidate.ToString());
            }

            if (isObjectToValidateNull || (logError != negated))
            {
                LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
            }
}

This uses basic integration with the config tool which explains why the regex pattern, regexoption and the negated variables are being populated with items from the attributes parameter.  In the configuration tool, you specify values for them using the Attributes property collection.  If you want full integration with the configuration tool, please refere to the documentation.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com