AspNet pseudo-validation message

Topics: Validation Application Block
Aug 7, 2007 at 11:44 PM
Hi,

As mentioned by Tom in his post here: http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=9085
"you can create a pseudo-validation error" which is great, but I'd like to use the MessageTemplateResourceName to cater for messages in different languages. Is this possible?

Thanks.

Matt.
Aug 8, 2007 at 12:02 AM
Hi Matt,

You would likely use resources to generate that pseudo error, just not using the MessageTemplateResourceName. Just create your resource file, and when populating the ConversionErrorMessage on the event access one of the properties of the resource class generated for you by VS.

Regards,
Fernando
Aug 8, 2007 at 12:55 AM
Thanks Fernando.

I really wanted to use the ErrorMessages.resx file in the same class library as the entity class with the validation attributes.

I've found another way around this. I'm using a convert method and ignoring any errors and simply decorating the property with a RangeValidator... seems odd, but works.

On the UI:
<asp:TextBox id="txtBedrooms" runat="server" MaxLength="10" CssClass="textBox"></asp:TextBox>
<entlib:PropertyProxyValidator id="ppvBedrooms" runat="server" ControlToValidate="txtBedrooms" PropertyName="Bedrooms" SourceTypeName="MyNamespace.ResidentialProperty" CssClass="validator" Text="*" OnValueConvert="ValueConvertToInt32" />

On the base page:

protected void ValueConvertToInt32(object sender, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.ValueConvertEventArgs e)
{
try
{
e.ConvertedValue = Convert.ToInt32(e.ValueToConvert);
}
catch
{
// Don't care for some reason.
}
}


On the entity class:


/// <summary>
/// Gets or sets the bedrooms.
/// </summary>
RangeValidator(1, RangeBoundaryType.Inclusive, int.MaxValue, RangeBoundaryType.Inclusive, MessageTemplateResourceName = "InvalidNumber", MessageTemplateResourceType = typeof(ErrorMessages), Tag = "Bedrooms")
public int Bedrooms {
get { return _bedrooms; }
set { _bedrooms = value; }
}

Matt.