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 all 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 @@ -15,6 +15,7 @@
using ReactiveUI.Validation.ValidationBindings;
using Splat;

// ReSharper disable once CheckNamespace
namespace ReactiveUI.Validation.Extensions
{
/// <summary>
Expand All @@ -30,7 +31,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,13 +43,28 @@ 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)
where TView : IViewFor<TViewModel>
where TViewModel : class, IReactiveObject, IValidatableViewModel
{
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModelProperty is null)
{
throw new ArgumentNullException(nameof(viewModelProperty));
}

if (viewProperty is null)
{
throw new ArgumentNullException(nameof(viewProperty));
}

formatter ??= Locator.Current.GetService<IValidationTextFormatter<string>>() ??
SingleLineFormatter.Default;

Expand All @@ -67,7 +83,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,13 +97,28 @@ 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)
where TView : IViewFor<TViewModel>
where TViewModel : class, IReactiveObject, IValidatableViewModel
{
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModelProperty is null)
{
throw new ArgumentNullException(nameof(viewModelProperty));
}

if (viewProperty is null)
{
throw new ArgumentNullException(nameof(viewProperty));
}

formatter ??= Locator.Current.GetService<IValidationTextFormatter<string>>() ??
SingleLineFormatter.Default;

Expand All @@ -104,7 +135,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,13 +147,28 @@ 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)
where TView : IViewFor<TViewModel>
where TViewModel : class, IReactiveObject, IValidatableViewModel
{
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModelHelperProperty is null)
{
throw new ArgumentNullException(nameof(viewModelHelperProperty));
}

if (viewProperty is null)
{
throw new ArgumentNullException(nameof(viewProperty));
}

formatter ??= Locator.Current.GetService<IValidationTextFormatter<string>>() ??
SingleLineFormatter.Default;

Expand Down
58 changes: 52 additions & 6 deletions src/ReactiveUI.Validation.AndroidX/Extensions/ViewForExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ReactiveUI.Validation.ValidationBindings;
using Splat;

// ReSharper disable once CheckNamespace
namespace ReactiveUI.Validation.Extensions
{
/// <summary>
Expand All @@ -30,7 +31,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,13 +43,28 @@ 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)
where TView : IViewFor<TViewModel>
where TViewModel : class, IReactiveObject, IValidatableViewModel
{
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModelProperty is null)
{
throw new ArgumentNullException(nameof(viewModelProperty));
}

if (viewProperty is null)
{
throw new ArgumentNullException(nameof(viewProperty));
}

formatter ??= Locator.Current.GetService<IValidationTextFormatter<string>>() ??
SingleLineFormatter.Default;

Expand All @@ -67,7 +83,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,13 +97,28 @@ 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)
where TView : IViewFor<TViewModel>
where TViewModel : class, IReactiveObject, IValidatableViewModel
{
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModelProperty is null)
{
throw new ArgumentNullException(nameof(viewModelProperty));
}

if (viewProperty is null)
{
throw new ArgumentNullException(nameof(viewProperty));
}

formatter ??= Locator.Current.GetService<IValidationTextFormatter<string>>() ??
SingleLineFormatter.Default;

Expand All @@ -104,7 +135,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,13 +147,28 @@ 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)
where TView : IViewFor<TViewModel>
where TViewModel : class, IReactiveObject, IValidatableViewModel
{
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModelHelperProperty is null)
{
throw new ArgumentNullException(nameof(viewModelHelperProperty));
}

if (viewProperty is null)
{
throw new ArgumentNullException(nameof(viewProperty));
}

formatter ??= Locator.Current.GetService<IValidationTextFormatter<string>>() ??
SingleLineFormatter.Default;

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
Loading