Nullable string with length validation issue

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
andrei.sbarnea
Posts: 4
Joined: Tue 29 Oct 2013 05:44

Nullable string with length validation issue

Post by andrei.sbarnea » Tue 29 Oct 2013 05:59

Configuration: Entity developer - entity framework - dbcontext

A Nullable string with length validation will trigger validation errors if the string is null. Our intention is to apply the length validation only if the field is not null, leaving the possibility for the field to be null. Is this possible ?

Also, how could we check only for max length without checking for min length ?

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: Nullable string with length validation issue

Post by Shalex » Thu 31 Oct 2013 15:10

andrei.sbarnea wrote:A Nullable string with length validation will trigger validation errors if the string is null.
Please set Nullable=True for the corresponding field in both (CSDL and SSDL) parts of the model. If this doesn't help, send us a small test project so that we can reproduce the issue in our environment.

andrei.sbarnea
Posts: 4
Joined: Tue 29 Oct 2013 05:44

Re: Nullable string with length validation issue

Post by andrei.sbarnea » Fri 22 Nov 2013 07:06

Nullable is set in both places.
I think the issue is similar to: http://forums.devart.com/viewtopic.php?f=1&t=27430
only that we are using MySQL and enterprise validation block.

In this particular case the attribute added to the field is:
[Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator(0, Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeBoundaryType.Ignore, 255, Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeBoundaryType.Inclusive)]
[System.Runtime.Serialization.DataMember]
public virtual string TheProperty {
...
}

andrei.sbarnea
Posts: 4
Joined: Tue 29 Oct 2013 05:44

Re: Nullable string with length validation issue

Post by andrei.sbarnea » Sun 24 Nov 2013 10:37

Composite validators are not supported with enterprise library attributes.
Therefore both logic methods with: IgnoreNulls or CompositeValidator(.Or) will not work. I modified the template and confirmed this by getting not a valid value validator exception for both of them.
The only workaround that I could find (to keep using attributes) is to introduce nullable support through wrappers over the actual used value attributes.
For example: StringLengthValidator(original arguments) becomes NullableStringLengthValidator(bool nullable, original arguments), and DoValidate method take in account "nullable" and does not log validation errors if (nullable && objectToValidate == null).

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: Nullable string with length validation issue

Post by Shalex » Mon 25 Nov 2013 16:31

This behaviour is caused by Microsoft's StringLengthValidator attribute, so we cannot change it.

From our point of view, you have implemented a correct workaround by using a custom attribute. You can add this attribute to your model in the following way: navigate to Model > Settings > Attributes > select the assembly with your custom attribute and select the attribute in the window below, press OK. The corresponding attribute should be available now in the Attributes menu of the class property.

andrei.sbarnea
Posts: 4
Joined: Tue 29 Oct 2013 05:44

Re: Nullable string with length validation issue

Post by andrei.sbarnea » Mon 27 Jan 2014 07:39

We know of this approach but for us is better to just modify the template. It would be nice that such attributes would be selectable in property edit window and viewable directly in the properties panel, it is not of much use that the panel shows a collection of attributes - in order to check assigned attributes the user needs to do too many clicks per property to get to the actual listing, not productive at all.

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: Nullable string with length validation issue

Post by Shalex » Mon 27 Jan 2014 17:12

Thank you for your feedback. Please suggest your idea at http://devart.uservoice.com/forums/3867 ... -developer.

Post Reply