简体   繁体   中英

WPF IDataErrorInfo and NHibernate Validation - how to trigger a Validate?

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

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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM