Validating entries with OnValidateFieldValue

Posts   
 
    
Drakon
User
Posts: 6
Joined: 06-Oct-2014
# Posted on: 06-Oct-2014 13:08:46   

When reading this post be aware that I am very new to WPF and extremely new to LLBLGen. I am working on the assumption that there are no stupid questions simple_smile

I am trying to use LLBLGen's validation feature in a WPF application. I have an entity that I want to validate and have created a partial class with a couple of validation checks in it as follows.

using Mtl.ControlCentre.DataModel.HelperClasses;

namespace Mtl.ControlCentre.DataModel.EntityClasses
{
    public partial class PersonnelEntity
    {
        protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            bool toReturn = true;
            switch ((PersonnelFieldIndex)fieldIndex)
            {
                case PersonnelFieldIndex.PersonnelNo:

                    //Can't be null
                    toReturn = (!string.IsNullOrEmpty(value.ToString()));

                    if (!toReturn)
                    {
                        SetEntityFieldError(PersonnelFieldIndex.PersonnelNo.ToString(), "Must enter a personnel number",
                            false);
                    }
                    else
                        SetEntityFieldError(PersonnelFieldIndex.PersonnelNo.ToString(), "", false);
                    break;
                case PersonnelFieldIndex.FirstName:
                    //Can't have over 50 characters
                    toReturn = (value.ToString().Length <= PersonnelFields.FirstName.MaxLength);

                    if (!toReturn)
                        SetEntityFieldError(PersonnelFieldIndex.FirstName.ToString(), "Can't exceed 50 characters", false);
                    break;
                default:
                    toReturn = true;
                    break;
            }
            return toReturn;
        }
    }
}

The fields in question look like

<Label Grid.Row="3" Grid.Column="0" Content="Forename/s:" VerticalAlignment="Center" HorizontalAlignment="Right" />
            <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding PersonnelEntity.FirstName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" MinWidth="200" />

The first field, PersonnelNo is a required field and the second, FirstName, is limited to 50 characters. The functionality I want is that when the user leaves the field, they are notified of the error using IDataErrorInfo.

What is happening in practice is when over 50 characters are used a database exception is thrown by the generated PersonnelEntity class and my validation in my partial class is not being called.

If I reduce the maximum no of characters to, say, 20 then the excess characters are removed and no notification given. While this is not ideal even this happening with 50 characters is preferable to throwing a database exception.

Ideally I want the user to be able to see a red border around the incorrect field and have access to the error message I set in the validation code to allow display to the user.

If someone could give me a pointer in the right direction I would be most appreciative.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 06-Oct-2014 19:29:06   

That's because there is a built up to n validation logic that is executed before your method is called. This is why a truncation might take place if the string is larger than life the size denoted in the designer. You can bypass the validation logic, please check validation

Also please put a breakpoint at the start of your method and see if it gets called.

Drakon
User
Posts: 6
Joined: 06-Oct-2014
# Posted on: 15-Oct-2014 12:53:38   

Sorry it took me so long to respond. I've been busy with other tasks. I will investigate bypassing the validation logic and see how that works out.

Thanks Walaa.