MistyK
MistyK

Reputation: 6222

ReactiveValidatedObject with DataAnnotation force revalidation of property

I am trying to validate property only if another property is set to true. I am using RequiredIfAttribute which is taken from another webpage.

[RequiredIf("PropertyA",true)]
public string PropertyB
{
    get  
    {
        return _PropertyB;
    }
    set  
    {
        this.RaiseAndSetifChanged(x => x.PropertyB, value);
    }
}

RequiredIf attribute is checking if PropertyA is set to true then validate otherwise it skips validation and return Success. It's working like a charm but the problem is that it works only if PropertyB is changed but it doesn't know that it needs to refresh when PropertyA is changed. So I am trying to force the update when PropertyA is changed like this:

this.ObservableForProperty(x => x.PropertyA).Subscribe(obj =>
{
  this.RaisePropertyChanged(x=>x.PropertyB);
})

but it doesn't work - nothing happens. I think it's ignored because the value has not changed.

There is another approach which works but is rather a workaround than solution:

this.ObservableForProperty(x=>x.PropertyA).Subscribe(obj =>
{
 var temp = PropertyB;
 PropertyB = "anything"; //it forces revalidation
 PropertyB = temp; // it forces revalidation
})

Upvotes: 1

Views: 145

Answers (1)

Vinod Kumar
Vinod Kumar

Reputation: 408

I hope Binding PropertyA with PropertyB will help you in this case.

Upvotes: 1

Related Questions