CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Custom Busines Rule for numeric property

rated by 0 users
Answered (Verified) This post has 1 verified answer | 2 Replies | 0 Followers

Not Ranked
14 Posts
Kevin posted on Wed, Jul 27 2011 2:53 AM

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

Answered (Verified) Verified Answer

Top 10 Contributor
1,772 Posts
Verified by Kevin

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

 

 

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 10 Contributor
1,772 Posts
Verified by Kevin

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

 

 

Jonny Bekkum, Norway CslaContrib Coordinator

Not Ranked
14 Posts
Kevin replied on Wed, Jul 27 2011 3:27 AM

Thank you Jonny for your fast replay :-)

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

Page 1 of 1 (3 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems