Custom Busines Rule for numeric property

Custom Busines Rule for numeric property

Old forum URL: forums.lhotka.net/forums/t/10564.aspx


BruderKotlett posted on Wednesday, July 27, 2011

Hello,

I've a Custom Rules that checks if the user entered a valid numerical value (Double.TryParse)

I override the AddBusinessRules Method and added the rule like this:

BusinessRules.AddRule(new Business.CustomRules.Numerical(PreislistePos.PreisNormalProperty));

It works fine, as long as the user enters a numeric value. If the user types in characters, the rule doesn't checks the entered value. And the Property Status will not be shown.

Property:

        public static readonly PropertyInfo<decimal> PreisNormalProperty = RegisterProperty<decimal>(c => c.PreisNormal);
        [Display(ResourceType = typeof(Asego), Name = "PreisNormalText")]
        public decimal PreisNormal
        {
            get { return GetProperty(PreisNormalProperty); }
            set { SetProperty(PreisNormalProperty, value); }
        }

XAML:

<TextBox Grid.Column="2" Height="23" Name="preisNettoTextBox" Text="{Binding Path=CurrentSelectedPreislistePos.PreisNetto, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Margin="10,4,30,5" BorderBrush="{Binding Path=IsValid, ElementName=preisNettoStatus, Converter={StaticResource validbrushConverter}}" />

<csla:PropertyStatus x:Name="preisNettoStatus" Property="{Binding Path=PreisNetto, Mode=TwoWay}" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,10,0" IsManipulationEnabled="True" DataContext="{Binding Path=CurrentSelectedPreislistePos}" />

 

Now my question is, how to "fire" the rule if a user enter a character value in the TextBox?

Thanks and kind regards

Kevin

JonnyBee replied on Wednesday, July 27, 2011

Hi,

The default value converter is unable to transform the text input from user to a decimal value and so the value is never set in the BO numeric property.

You could create your own value converter and set a "non valid" numerical value in the BO if the input text can not be parsed to a decimal.

In short - 

A: your BO property is decimal - and you must use a custom value converter to convert a not valid text to a numeric value

B: You could change the BO value to a string and add extra logic in Rules/DAL to convert to/from string-numeric value.

Good info on ValueConverter can be found here: http://www.wpftutorial.net/ValueConverters.html

 

 

BruderKotlett replied on Wednesday, July 27, 2011

Thank you Jonny for your fast replay :-)

This will work for me, I think I use the "Converter-Option".

Copyright (c) Marimer LLC