Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Initial List Validation / Getting IsValid #450

Open
mysteryx93 opened this issue May 25, 2022 · 1 comment
Open

[Question] Initial List Validation / Getting IsValid #450

mysteryx93 opened this issue May 25, 2022 · 1 comment

Comments

@mysteryx93
Copy link

mysteryx93 commented May 25, 2022

I'm trying to do something very simple. I want to validate that the Sources ObservableCollection is not empty.

var sourceNotEmpty = Sources
    .ToObservableChangeSet(x => x.Path)
    .ToCollection()
    .Select(x => x.Any());
this.ValidationRule(x => x.Sources,
    sourceNotEmpty,
    "You must select source files or folders to encode");

Testing GetErrors works except for this test. It fails because the ValidationRule hasn't yet been evaluated. If I add an item and then remove it, then it works.

[Fact]
public void GetErrors_SourceEmpty_Error()
{
    var errors = Model.GetErrors(nameof(Model.Sources));

    Assert.Single(errors);  // errors is empty
}

Then, how do I get IsValid? Before doing some work in the ViewModel, I first need to check "IsValid" but haven't yet found the way to get the current value. LastAsync won't work if it hasn't yet been evaluated.

[Fact]
public void IsValid_MissingData_ReturnsFalse()
{
    bool? result = null;

    Model.IsValid().Do(x => result = x);
    
    Assert.False(result);
}

These 2 things are extremely simple but there's probably something I'm missing with the whole paradigm?

Adding to this, on the UI, I probably don't want to display all the "field missing" errors right away, but only when he tries to click Submit or re-empties a field after typing in it.

@mysteryx93
Copy link
Author

mysteryx93 commented May 26, 2022

Alright I've made progress on this after spending the day reading through the docs. Can you tell me whether I'm doing it right?

To get initial list validation, calling Sources.Clear() works.

var sourceNotEmpty = Sources
    .ToObservableChangeSet(x => x.Path)
    .ToCollection()
    .Select(x => x.Any());
this.ValidationRule(x => x.Sources,
    sourceNotEmpty,
    "You must select source files or folders to encode");
this.Sources.Clear();

To get IsValid...

constructor...
    _isValid = this.IsValid().ToProperty(this, x => x.IsValidValue);
}

readonly ObservableAsPropertyHelper<bool> _isValid;
public bool IsValidValue
{
    get { return _isValid.Value; }  
}	

Is this the right way? I would think that this should instead be in the base ReactiveValidationObject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant