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

feature: Allow the ViewModel to be Null in BindValidation #152

Merged
merged 4 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class ViewForExtensions
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
/// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam>
/// <param name="view">IViewFor instance.</param>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModel">ViewModel instance. Can be null, used for generic type resolution.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="viewProperty">View property to bind the validation message.</param>
/// <param name="formatter">
Expand All @@ -42,7 +42,7 @@ public static class ViewForExtensions
[SuppressMessage("Design", "CA1801: Parameter unused", Justification = "Used for generic resolution.")]
public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty>(
this TView view,
TViewModel viewModel,
TViewModel? viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
TextInputLayout viewProperty,
IValidationTextFormatter<string>? formatter = null)
Expand All @@ -67,7 +67,7 @@ public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty>(
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
/// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam>
/// <param name="view">IViewFor instance.</param>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModel">ViewModel instance. Can be null, used for generic type resolution.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="viewProperty">View property to bind the validation message.</param>
/// <param name="formatter">
Expand All @@ -81,7 +81,7 @@ public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty>(
[SuppressMessage("Design", "CA1801: Parameter unused", Justification = "Used for generic resolution.")]
public static IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty>(
this TView view,
TViewModel viewModel,
TViewModel? viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
TextInputLayout viewProperty,
IValidationTextFormatter<string>? formatter = null)
Expand All @@ -104,7 +104,7 @@ public static IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty
/// <typeparam name="TView">IViewFor of TViewModel.</typeparam>
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
/// <param name="view">IViewFor instance.</param>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModel">ViewModel instance. Can be null, used for generic type resolution.</param>
/// <param name="viewModelHelperProperty">ViewModel's ValidationHelper property.</param>
/// <param name="viewProperty">View property to bind the validation message.</param>
/// <param name="formatter">
Expand All @@ -116,7 +116,7 @@ public static IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty
[SuppressMessage("Design", "CA1801: Parameter unused", Justification = "Used for generic resolution.")]
public static IDisposable BindValidation<TView, TViewModel>(
this TView view,
TViewModel viewModel,
TViewModel? viewModel,
Expression<Func<TViewModel?, ValidationHelper>> viewModelHelperProperty,
TextInputLayout viewProperty,
IValidationTextFormatter<string>? formatter = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class ViewForExtensions
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
/// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam>
/// <param name="view">IViewFor instance.</param>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModel">ViewModel instance. Can be null, used for generic type resolution.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="viewProperty">View property to bind the validation message.</param>
/// <param name="formatter">
Expand All @@ -42,7 +42,7 @@ public static class ViewForExtensions
[SuppressMessage("Design", "CA1801: Parameter unused", Justification = "Used for generic resolution.")]
public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty>(
this TView view,
TViewModel viewModel,
TViewModel? viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
TextInputLayout viewProperty,
IValidationTextFormatter<string>? formatter = null)
Expand All @@ -67,7 +67,7 @@ public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty>(
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
/// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam>
/// <param name="view">IViewFor instance.</param>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModel">ViewModel instance. Can be null, used for generic type resolution.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="viewProperty">View property to bind the validation message.</param>
/// <param name="formatter">
Expand All @@ -81,7 +81,7 @@ public static IDisposable BindValidation<TView, TViewModel, TViewModelProperty>(
[SuppressMessage("Design", "CA1801: Parameter unused", Justification = "Used for generic resolution.")]
public static IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty>(
this TView view,
TViewModel viewModel,
TViewModel? viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
TextInputLayout viewProperty,
IValidationTextFormatter<string>? formatter = null)
Expand All @@ -104,7 +104,7 @@ public static IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty
/// <typeparam name="TView">IViewFor of TViewModel.</typeparam>
/// <typeparam name="TViewModel">ViewModel type.</typeparam>
/// <param name="view">IViewFor instance.</param>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModel">ViewModel instance. Can be null, used for generic type resolution.</param>
/// <param name="viewModelHelperProperty">ViewModel's ValidationHelper property.</param>
/// <param name="viewProperty">View property to bind the validation message.</param>
/// <param name="formatter">
Expand All @@ -116,7 +116,7 @@ public static IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty
[SuppressMessage("Design", "CA1801: Parameter unused", Justification = "Used for generic resolution.")]
public static IDisposable BindValidation<TView, TViewModel>(
this TView view,
TViewModel viewModel,
TViewModel? viewModel,
Expression<Func<TViewModel?, ValidationHelper>> viewModelHelperProperty,
TextInputLayout viewProperty,
IValidationTextFormatter<string>? formatter = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,19 @@ namespace ReactiveUI.Validation.Extensions
[System.Obsolete("This method is a part of ReactiveUI internals and will be removed from ReactiveUI" +
".Validation public API soon.")]
public static System.IDisposable BindToDirect<TTarget, TValue>(System.IObservable<TValue> @this, TTarget target, System.Linq.Expressions.Expression viewExpression) { }
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel?, ReactiveUI.Validation.Helpers.ValidationHelper>> viewModelHelperProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel?, ReactiveUI.Validation.Helpers.ValidationHelper>> viewModelHelperProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
public static System.IDisposable BindValidation<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidation<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[System.Obsolete("This method is no longer required, BindValidation now supports multiple validatio" +
"ns.")]
public static System.IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,19 @@ namespace ReactiveUI.Validation.Extensions
[System.Obsolete("This method is a part of ReactiveUI internals and will be removed from ReactiveUI" +
".Validation public API soon.")]
public static System.IDisposable BindToDirect<TTarget, TValue>(System.IObservable<TValue> @this, TTarget target, System.Linq.Expressions.Expression viewExpression) { }
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel?, ReactiveUI.Validation.Helpers.ValidationHelper>> viewModelHelperProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidation<TView, TViewModel, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel?, ReactiveUI.Validation.Helpers.ValidationHelper>> viewModelHelperProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
public static System.IDisposable BindValidation<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidation<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[System.Obsolete("This method is no longer required, BindValidation now supports multiple validatio" +
"ns.")]
public static System.IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
public static System.IDisposable BindValidationEx<TView, TViewModel, TViewModelProperty, TViewProperty>(this TView view, TViewModel? viewModel, System.Linq.Expressions.Expression<System.Func<TViewModel, TViewModelProperty>> viewModelProperty, System.Linq.Expressions.Expression<System.Func<TView, TViewProperty>> viewProperty, ReactiveUI.Validation.Formatters.Abstractions.IValidationTextFormatter<string>? formatter = null)
where TView : ReactiveUI.IViewFor<TViewModel>
where TViewModel : class, ReactiveUI.IReactiveObject, ReactiveUI.Validation.Abstractions.IValidatableViewModel { }
}
Expand Down
17 changes: 15 additions & 2 deletions src/ReactiveUI.Validation.Tests/Models/TestView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ namespace ReactiveUI.Validation.Tests.Models
/// <summary>
/// Mocked View.
/// </summary>
public class TestView : IViewFor<TestViewModel>
public class TestView : ReactiveObject, IViewFor<TestViewModel>
{
private TestViewModel _viewModel;

/// <summary>
/// Initializes a new instance of the <see cref="TestView"/> class.
/// </summary>
public TestView()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TestView"/> class.
/// </summary>
Expand All @@ -27,7 +36,11 @@ object IViewFor.ViewModel
}

/// <inheritdoc/>
public TestViewModel ViewModel { get; set; }
public TestViewModel ViewModel
{
get => _viewModel;
set => this.RaiseAndSetIfChanged(ref _viewModel, value);
}

/// <summary>
/// Gets or sets the Name Label which emulates a Text property (eg. Entry in Xamarin.Forms).
Expand Down
30 changes: 30 additions & 0 deletions src/ReactiveUI.Validation.Tests/ValidationBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,36 @@ public void ShouldBindValidationRuleEmittingValidationStatesGeneric()
Assert.DoesNotContain(viewModelIsBlockedMessage, view.NameErrorContainer.Text, comparison);
}

/// <summary>
/// Verifies that we support nullable view model properties.
/// </summary>
[Fact]
public void ShouldSupportDelayedViewModelInitialization()
{
var view = new TestView
{
NameErrorLabel = string.Empty,
NameErrorContainer = { Text = string.Empty }
};

view.Bind(view.ViewModel, x => x.Name, x => x.NameLabel);
view.BindValidation(view.ViewModel, x => x.Name, x => x.NameErrorLabel);
view.BindValidation(view.ViewModel, x => x.NameErrorContainer.Text);

Assert.Empty(view.NameErrorLabel);
Assert.Empty(view.NameErrorContainer.Text);

const string errorMessage = "Name shouldn't be empty.";
var viewModel = new TestViewModel();
viewModel.ValidationRule(x => x.Name, x => !string.IsNullOrWhiteSpace(x), errorMessage);
view.ViewModel = viewModel;

Assert.NotEmpty(view.NameErrorLabel);
Assert.NotEmpty(view.NameErrorContainer.Text);
Assert.Equal(errorMessage, view.NameErrorLabel);
Assert.Equal(errorMessage, view.NameErrorContainer.Text);
}

private class CustomValidationState : IValidationState
{
public CustomValidationState(bool isValid, string message)
Expand Down
Loading