BUG: StringLengthValidator & neutral culture

Topics: Validation Application Block
May 29, 2007 at 9:36 PM
Hi,

I get an exception when trying to use the StringLenthValidator in combination with my app running CurrentUICulture = "en".

The error is: System.NotSupportedException: Culture 'en' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.

The culprit is the StringLengthValidator.GetMessage method which again calls System.String.Format.

return string.Format(CultureInfo.CurrentUICulture,
this.MessageTemplate,
objectToValidate,
key,
this.Tag,
this.rangeChecker.LowerBound,
this.rangeChecker.LowerBoundType,
this.rangeChecker.UpperBound,
this.rangeChecker.UpperBoundType);

So my question now is, is this behaviour by design?

Stacktrace:

at System.Globalization.CultureInfo.CheckNeutral(CultureInfo culture)
at System.Globalization.CultureInfo.get_NumberFormat()
at System.Globalization.NumberFormatInfo.GetInstance(IFormatProvider formatProvider)
at System.Int32.ToString(String format, IFormatProvider provider)
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator.GetMessage(Object objectToValidate, String key) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validators\StringLengthValidator.cs:line 185
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator.DoValidate(String objectToValidate, Object currentTarget, String key, ValidationResults validationResults) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validators\StringLengthValidator.cs:line 168
at Microsoft.Practices.EnterpriseLibrary.Validation.Validator`1.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validator.Generic.cs:line 97
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validators\AndCompositeValidator.cs:line 57
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValueAccessValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validators\ValueAccessValidator.cs:line 72
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validators\AndCompositeValidator.cs:line 57
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValidatorWrapper.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validators\ValidatorWrapper.cs:line 74
at Microsoft.Practices.EnterpriseLibrary.Validation.Validator.Validate(Object target) in c:\ELBUILD\Trunk\Source\App Blocks\Src\Validation\Validator.cs:line 53
May 29, 2007 at 9:49 PM
A quick run through of the App Block src reveals that this problem is not isolated to the StringLengthValidator. It's littered with calls to the String.Format(IFormatProvider, ...), which of course will not accept neutral cultures.

Do I need to re-think my localization strategy here? I was planning to localize purely based on neutral cultures since that is our requirement.

May 31, 2007 at 2:38 PM
I think this is bug which should be fixed (and i filled it as bug in issue tracker already)
Main problem is when using VAB in web application with CurrentUICulture set to "auto"
In this case, you do not have control over user's browser Language settings....

just my 2c
May 31, 2007 at 4:13 PM
I checked with the developers and this is a bug. They did a sweep through the block to make sure everything was CultureInfo.CurrentCulture, but they obviously missed some.

I see the WorkItem Here: http://www.codeplex.com/entlib/WorkItem/View.aspx?WorkItemId=10527. Please vote on it to make sure it gets noticed.

I don't know when we will see another maintenance release, but the problem is logged for them to see.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
May 31, 2007 at 8:09 PM
Thanks for the heads up, Dave!

I have cast my vote :) Keep up the great work...