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
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.