accessing built-in validation logic errors in partial class

Posts   
 
    
Posts: 4
Joined: 11-Dec-2009
# Posted on: 11-Dec-2009 10:48:17   

Good day

I am using LLBLGen Pro 2.6, WPF, C#, SQL Server 2005.

I have a partial entity class in which I override OnValidateFieldValue. eg. protected override bool OnValidateFieldValue(int fieldIndex, object value) { bool toReturn = true; switch ((TraderLimitsFieldIndex)fieldIndex) { case TraderLimitsFieldIndex.Limit: toReturn = ((double)value > 0); if (toReturn == false) SetEntityFieldError("Limit", "Limit must be positive", false); break; default: toReturn = true; break; } return toReturn; }

within the same partial class I have method IsValid(): public Boolean IsValid() { if (DataErrorInfoErrorsPerField == null) return true; else return (DataErrorInfoErrorsPerField.Count == 0); }

On the WPF-side I use a MVVM architecture, where I check to see if the form can be saved by calling my entities new IsValid() method. If false I disable the control.

This works fine, but... when I delete the bound value's data from the window, the form textbox validates correctly by displaying the relevant llblgen built-in logic error, but my control does not disable. My question is, how can I get access to this error from within my IsValid method in order to check for the built-in validation errors also?

the xaml looks like this: <TextBox FontWeight="Normal" FontSize="11" Width="150" VerticalContentAlignment="Center" Text="{Binding Path=Limit, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, StringFormat='{0:#,##0}', ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" fw:NumericTextBoxBehavior.IsEnabled="True"/>

Regards Johan

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 11-Dec-2009 12:13:33   

within the same partial class I have method IsValid(): public Boolean IsValid() { if (DataErrorInfoErrorsPerField == null) return true; else return (DataErrorInfoErrorsPerField.Count == 0); }

On the WPF-side I use a MVVM architecture, where I check to see if the form can be saved by calling my entities new IsValid() method. If false I disable the control.

This works fine, but... when I delete the bound value's data from the window, the form textbox validates correctly by displaying the relevant llblgen built-in logic error, but my control does not disable. My question is, how can I get access to this error from within my IsValid method in order to check for the built-in validation errors also?

The built-in validation kicks when a value is set to an entity field, and if it's not valid the value is not set and an error is added to the DataErrorInfoErrorsPerField Dictionary, which you are already checking. So as far as I can see it, when the user removes the data from the TextBox, this propagates to the entity Field, and it seems that the built-in validation succeeds this time, and thus control is passed to your custom validation code.

So would you please place a breakpoint in the OnValidateFieldValue and see if it gets hit in this specific scenario.

Posts: 4
Joined: 11-Dec-2009
# Posted on: 11-Dec-2009 12:29:28   

Thanks for the reply.

I did try the break-point route , and yes, it enters OnValidateFieldValue, when the value changes, but not when deleted.

I tested as follows: 1. change current value to valid value -> shows no validation error on xaml and IsValid() returns true 2. change current value to 0 -> shows validation error as set by OnValidateFieldValue on xaml and IsValid() returns false; 3. change back to valid value, same as 1. 4. then, highlight value and press delete, effectively clearing the textbox -> shows validation error on xaml (build in error : Input string was not in correct ...), but, never enters OnValidateFieldValue and IsValid() still returns true

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 11-Dec-2009 12:38:23   

So if you examine the entity field value after step 4 would you still see the valid value set at step 3?

Posts: 4
Joined: 11-Dec-2009
# Posted on: 11-Dec-2009 13:09:17   

Yes, I added a debug.writeln to the property bound to my command where it checks isvalid. I enter eg. 1, which works, then I highlight it and delete, when i tab off debug is still 1, so it seems that it never actually changes the entity's field value

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 11-Dec-2009 14:15:21   

That's what I've expecte. It sounds like no databinding is kickin' when you clear the textBox. So I guess this has nothing to do with LLBLGen and must be something in the databinding code.

Posts: 4
Joined: 11-Dec-2009
# Posted on: 11-Dec-2009 15:59:07   

Hi

Thanks for the help.

I fixed it by doing the following: I wrote a valueconverter to convert to string to a double and return 0 if null and added this to my binding. This solved the exception from being thrown, which in turn caused the property to be updated correctly causing the binding to now work.

[ValueConversion(typeof(string), typeof(double))]
public class NullDoubleConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return value;
    }

    /// <summary>
    /// Convert back, but its not implemented
    /// </summary>
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if ( (value == null) || ((string)value).Length == 0)
            return 0;

        return Double.Parse((String)value, NumberStyles.Float); ;
    }
}

Regards