Search code examples
c#reactiveui

ReactiveUI.Validations and nested validation


I would like to know if ReactiveUI.Validation supports nested validations. Eg. a parent ViewModel that is valid only when its children ViewModels are valid.

Something like this:

public class ParentViewModel : ReactiveValidationObject
{
    public Parent()
    {
         var child1 = new ChildViewModel();
         var child2 = new ChildViewModel();

         this.ValidationRule(child1);
         this.ValidationRule(child2);
    }
}

Solution

  • You can use the IsValid() extension method to get an IObservable<bool> instance to get the valid state of the inner model. Use it in your "main" model at your ValidationRule() method with the ValidationRule(IObservable<bool>, string) overload. See the following example:

    public class ParentViewModel : ReactiveValidationObject
    {
        public Parent()
        {
             var child1 = new ChildViewModel();
             var child2 = new ChildViewModel();
    
             this.ValidationRule(child1.IsValid(), "the first child model is invalid");
             this.ValidationRule(child2.IsValid(), "the second child model is invalid");
        }
    }