views:

231

answers:

2

Hi,

I recently plugged the NHibernate validation into my app, I've decorated the properties of my domain objects with the NHibernate attributes as so ...

    [NotEmpty, Length(Min = 1, Max = 40)]
    public string Description { get; set; }

I've also implemented IDataErrorInfo on my Domain Object ...

    public string this[string columnName]
    {
        get
        {
            var result = new ValidatorEngine().Validate(this);
            _invalidValues = result.Where(x => x.PropertyName == columnName).Select(x => x.Message);
            return _invalidValues.FirstOrDefault();
        }

    }

    public string Error
    {
        get
        {
            return string.Empty;
        }
    }

The XAML looks like this

 <TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Path=Entity.Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" IsEnabled="{Binding IsEditable}" ></TextBox>

My issue is that when I create a new instance of my Domain object then the validate is not being called, as effectively the properties (such as the Description in my example) have not changed.

I was going to write a method to use reflection and set the properties to what they are already equal to in order to trigger the validate, but this dosnt seem a particularly efficient approach!!

Can someone put me back on track please?

Cheers,

Andy

A: 

It was because the properties were Null and I needed another NHibernate validation decorator to take account of that (NotNullNotEmpty) rather than the NotEmpty I had used.

 [NotNullNotEmpty, Length(Min = 1, Max = 40)]
 public string Description { get; set; }
Andy Clarke