0

I'm new to RxUI, coming from Jounce, trying to do field validation the best way. Cant seem to find docs on the best way to do validation in RxUI.

You have some view model that derives from ReactiveValidatedObject.

It has properties like:

    [Required]
    [StringLength(9,MinimumLength=9)]
    public string CUSIP
    {
        get { return _CUSIP; }
        set { this.RaiseAndSetIfChanged(x => x.CUSIP, value); }
    }

How do I make sure that validation errors show in my UI on the controls that have the errors when I go to run the save command?

        this.SaveCommand = new ReactiveCommand();
        this.SaveCommand.Subscribe(_ =>
            {
            });

... I'm used to Jounce and doing stuff like:

    private string _oldPassword;
    public string OldPassword
    {
        get { return _oldPassword; }
        set { _oldPassword = value;
        ValidateOldPassword();
        RaisePropertyChanged(() => OldPassword);
            }
    }

    private void ValidateOldPassword()
    {
        ClearErrors(() => OldPassword);
        if (string.IsNullOrEmpty(OldPassword))
            SetError(() => OldPassword, "Old Password is required.");
        if (OldPassword != UserContext.Password)
            SetError(() => OldPassword, "Old Password is incorrect.");
    }

Thanks for any help. :)

Austin Harris
  • 1,676
  • 1
  • 17
  • 22

1 Answers1

1

So, in this case, the "correct way" to do this is via UserError:

var disp = UserError.RegisterHandler(error => {
     // TODO: Make this better :) 
     MessageBox.Show(error.ErrorMessage);
     return null;
});

// Unregister the error handler for the Window once it closes
this.OnClose += (o,e) => disp.Dispose();

this.SaveCommand
    .Where(_ => IsObjectValid())
    .Subscribe(_ -> SaveTheObject());

this.SaveCommand
    .Where(_ => !IsObjectValid())
    .Subscribe(_ -> UserError.Throw("The form is invalid"));
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • Maybe I'm missing something with this. My controls on the form do not get the little red box around them indicating that there is some kind of input error when the saveCommand is invoked. They only get the red box if the value changes from the UI. Example.. I have a decimal? property that defaults to null. On the from this shows an empty box. I have the Required, and Range validation attributes on it. Those do not seem to affect the UI unless the control that is bound to that property is interacted with. – Austin Harris Oct 05 '12 at 16:13
  • @PaulBetts I get an ambigious build error. ...: error CS0121: The call is ambiguous between the following methods or properties: 'ReactiveUI.Xaml.UserError.RegisterHandler(System.Func>)' and 'ReactiveUI.Xaml.UserError.RegisterHandler(System.Func>)' – kenny Oct 17 '12 at 17:43
  • this works though var disp = UserError.RegisterHandler(e => ErrorHandler(e)); } IObservable ErrorHandler(UserError userError) { // TODO: Make this better :) MessageBox.Show(userError.ErrorMessage); return null; } – kenny Oct 17 '12 at 17:58