Does framework validation always throws an exception?

Posts   
 
    
timbered
User
Posts: 80
Joined: 09-Feb-2020
# Posted on: 27-Dec-2024 22:15:35   

If I set a string field to a length longer than the Max Length set in the designer, does the framework always throw a "The value specified with cause an overflow error..." exception (unless I set the BuildInValidationBypassMode to one of the bypass flags, in which case, I have to check string length in my own logic)?

In other words, there's no way to have LlblGen set the IDataErrorInfo but not throw the exception that I then have to trap somehow?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 28-Dec-2024 09:19:16   

If you want to bypass the built-in validation, you have to set the Validator object on the entity (and set the bypass mode to BypassWhenValidatorPresent). Alternatively, you can override OnValidateFieldValue and do the validation there (and set the BuildInValidationBypassMode to Alwaysbypass).

If an error has been generated in the built-in validation code, it will set the error on the IDataErrorInfo for the field and the exception will notify bound controls that an error has occurred. So the exception is necessary.

Frans Bouma | Lead developer LLBLGen Pro
timbered
User
Posts: 80
Joined: 09-Feb-2020
# Posted on: 28-Dec-2024 17:51:15   

Previously, I've only used the DataErrorValidationRule on my (non-LlblGen) bindings, and didn't use exceptions or the ExceptionValidationRule before. Looks like that will change.

Thanks for your help!

timbered
User
Posts: 80
Joined: 09-Feb-2020
# Posted on: 29-Dec-2024 01:38:13   

Ok, I'm still missing something.

I'm doing WPF, NET Framework 4.8

I have a DevExpress TextEdit control bound to a string entity field, and I want to validate the string length, as well as a custom validation.

If I use the LlblGen validation, it will validate the length. So I put my custom validation in the IValidator ValidateFieldValue routine, where, if it's invalid, I set the EntityFieldError message and return false. To me, that should set the IDataErrorInfo stuff for this field, the bound DevExpress control should see that, and light up the error icon in the text box. But that doesn't happen. LlblGen doesn't set the IDataErrorInfo if I return False from ValidateFieldValue. It just skips setting the field, changes it back to it's original value, and moves on. I suppose this makes sense, on the data layer, to remove the offending data.

If I use the DevExpress validation (which is done either through Events in the code behind [yuck], or IDataErrorInfo), I can show the error icon with my custom string validation, but I don't have access to the original Entity field metadata, so I don't have access to it's MaxLength and can't put that check in my custom validator.

What would be the best way both a validation on the field data itself, as well as indicate to the control that there's an error on the underlying data?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 29-Dec-2024 09:28:56   

The idea is that if you do the validation yourself, you won't get the exception and IDataErrorInfo isn't set, so you have to do that yourself. To do that, use the entity's SetEntityFieldErrormethod. After that, throw the exception you want (or don't, if you don't want to).

Does that fix your issue? (The built-in validation code in question is in EntityCore.cs, ValidateValue which starts at 3310 )

Frans Bouma | Lead developer LLBLGen Pro
timbered
User
Posts: 80
Joined: 09-Feb-2020
# Posted on: 30-Dec-2024 17:09:50   

I did fix it, between your explanation and trying different scenarios. I ended up doing both - using the DevExpress event for my custom validation (I added one line in my code behind to call the method on my business object. I can live with that) and adding the ExceptionValidationRule for the length.

Thanks again for your help!