SetValue for non-nullable varchar Field

Posts   
 
    
kievBug
User
Posts: 105
Joined: 09-Jan-2009
# Posted on: 07-Mar-2009 17:21:54   

Hi,

I've tried to search for similar problems but can not find anything, however I think that the problem is typical.

I've entity with non-nullable field in it let's say Customer.Name: varchar(50) not null. I've an application that just sets this field with a value in a text box. I'd like to enforce validation for this field so it is required(user should specify something before saving it). I'm trying to use IDataErrorInfo for this purpose.

So basically I've a code like this:

customer.Name = ""

I assume that if the field is not null, valid values for it are everything except DbNull, String.Empty, or any string including only white characters.

Currently these values String.Empty, or any string including only white characters(not sure about DbNull) are valid values and do not add errors into IDataErrorInfo for that field.

What is the best way to add such validation? As you understand there are a lot of entities with the same fields(Products, Categories and so on), so solution should be general.

Thanks, Anton

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Mar-2009 20:47:54   

Hi Anton,

You could write a Validator class and inject it to "IEntity(2)" interface so all entities would apply to it. Then implement the ValidateFieldValue method. In there you could check whether the field is nullable, string and its value is "empty". Then you could set the error info. Something like: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=14970

For more info related to validation, read this.

David Elizondo | LLBLGen Support Team
kievBug
User
Posts: 105
Joined: 09-Jan-2009
# Posted on: 08-Mar-2009 23:33:09   

Hi David,

Thanks for the answer. Is it possible for entity have several validators injected by DI?

Let's say I want this validator be injected for all entities and for some entities I want to add some specific validation(let's say for Orders I want to add validation for OrderDate to be in some period). Is it possible to inject both validators and have both of them separated? I mean have NotNullableStringFieldValidator which will be generic and it will be injected to all entities and have another OrderDateValidator(which will be specific and injected only into Orders entity).

Thanks, Anton

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 09-Mar-2009 07:58:32   

Use inheritance for your validators.

[DependencyInjectionInfo(typeof(IEntity), "Validator", 
    ContextType = DependencyInjectionContextType.Singleton)]
public class GeneralValidator : ValidatorBase
{
}

[DependencyInjectionInfo(typeof(OrderEntity), "Validator", 
    ContextType = DependencyInjectionContextType.Singleton)]
public class OrderDateValidator : GeneralValidator 
{
}
kievBug
User
Posts: 105
Joined: 09-Jan-2009
# Posted on: 09-Mar-2009 13:48:21   

Ok thanks.