How to create a custom validator in VAB that checks two properties at once?

In my database, I have a composite unique key constraint (CustomerId, Name) for an object. I want to check if a unique constraint is being met before pushing changes to the database. The user can customize the Name attribute on the object, so I would like to create a custom validator that validates the name property, but I also need access to the CustomerId property. How to do it?

I'm using WCSF which means I'm stuck with Entlib 3.1 and VAB enabled, but I think it is possible to switch to EntLib 4.1 VAB without breaking WCSF.

Regards, Egil.

0


source to share


1 answer


The trick is to create a validator for your entity, not one of the properties of that entity. You can write your own check on your entity like this:

[HasSelfValidation]
public class MyEntity
{
    public int CustomerId { get; set; }
    public string Name { get; set; }

    [SelfValidation]
    public void ValidateRange(ValidationResults results)
    {
        bool isUnique = [query the database here]

        if (!isUnique)
        {
            results.AddResult(new ValidationResult(
                "CustomerId and Name are not unique", this, "", "", null));
        }
    }
}

      



I must say that I am not familiar with the VAB 3.1 feature set, so I am not 100% sure if this works in version 3.1. However, it works on 4.1. If you don't like self-assessment, you can also write a custom validator and include it in the config file. Have a look at fooobar.com/questions/737731 / ... for more information on how to do this.

0


source







All Articles