From d2108879e92677d6dc921a9d3ba13f8c7528c2d4 Mon Sep 17 00:00:00 2001 From: Andrei Misiukevich Date: Thu, 4 Feb 2021 23:00:35 +0100 Subject: [PATCH 1/3] Added IsNotValid property to validators --- .../Validators/ValidationBehavior.shared.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/ValidationBehavior.shared.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/ValidationBehavior.shared.cs index 43d96c294..772453384 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/ValidationBehavior.shared.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/ValidationBehavior.shared.cs @@ -6,8 +6,11 @@ namespace Xamarin.CommunityToolkit.Behaviors.Internals { public abstract class ValidationBehavior : BaseBehavior { + public static readonly BindableProperty IsNotValidProperty = + BindableProperty.Create(nameof(IsNotValid), typeof(bool), typeof(ValidationBehavior), false, BindingMode.OneWayToSource); + public static readonly BindableProperty IsValidProperty = - BindableProperty.Create(nameof(IsValid), typeof(bool), typeof(ValidationBehavior), true, BindingMode.OneWayToSource); + BindableProperty.Create(nameof(IsValid), typeof(bool), typeof(ValidationBehavior), true, BindingMode.OneWayToSource, propertyChanged: OnIsValidPropertyChanged); public static readonly BindableProperty ValidStyleProperty = BindableProperty.Create(nameof(ValidStyle), typeof(Style), typeof(ValidationBehavior), propertyChanged: OnValidationPropertyChanged); @@ -39,6 +42,12 @@ public bool IsValid set => SetValue(IsValidProperty, value); } + public bool IsNotValid + { + get => (bool)GetValue(IsNotValidProperty); + set => SetValue(IsNotValidProperty, value); + } + public Style ValidStyle { get => (Style)GetValue(ValidStyleProperty); @@ -124,6 +133,9 @@ protected override void OnViewPropertyChanged(object sender, PropertyChangedEven protected static void OnValidationPropertyChanged(BindableObject bindable, object oldValue, object newValue) => ((ValidationBehavior)bindable).UpdateState(false); + static void OnIsValidPropertyChanged(BindableObject bindable, object oldValue, object newValue) + => ((ValidationBehavior)bindable).OnIsValidPropertyChanged(); + static void OnValuePropertyChanged(BindableObject bindable, object oldValue, object newValue) { ((ValidationBehavior)bindable).OnValuePropertyChanged(); @@ -139,6 +151,9 @@ static object GetDefaultForceValidateCommand(BindableObject bindable) static object GetDefaultValuePropertyName(BindableObject bindable) => ((ValidationBehavior)bindable).DefaultValuePropertyName; + void OnIsValidPropertyChanged() + => IsNotValid = !IsValid; + void OnValuePropertyChanged() { if (isAttaching) From 5bf61f27e03a8eb1c32392eca6103052df29e22e Mon Sep 17 00:00:00 2001 From: Andrei Misiukevich Date: Thu, 4 Feb 2021 23:03:34 +0100 Subject: [PATCH 2/3] small fix --- .../Behaviors/Validators/MultiValidationBehavior.shared.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/MultiValidationBehavior.shared.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/MultiValidationBehavior.shared.cs index d304407a8..d5fc991ea 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/MultiValidationBehavior.shared.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/MultiValidationBehavior.shared.cs @@ -43,7 +43,7 @@ protected override bool Validate(object value) { c.Value = value; c.ForceValidate(); - return !c.IsValid; + return c.IsNotValid; }).Select(c => GetError(c)); if (!errors.Any()) From 6898307aaaec51e7605439d3cd20dcf870a80cf2 Mon Sep 17 00:00:00 2001 From: Andrei Misiukevich Date: Wed, 10 Feb 2021 01:59:30 +0100 Subject: [PATCH 3/3] Updated sample --- .../EmailValidationBehaviorPage.xaml | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/samples/XCT.Sample/Pages/Behaviors/EmailValidationBehaviorPage.xaml b/samples/XCT.Sample/Pages/Behaviors/EmailValidationBehaviorPage.xaml index e9d055e70..f9fef55cd 100644 --- a/samples/XCT.Sample/Pages/Behaviors/EmailValidationBehaviorPage.xaml +++ b/samples/XCT.Sample/Pages/Behaviors/EmailValidationBehaviorPage.xaml @@ -15,11 +15,21 @@ Spacing="50" VerticalOptions="CenterAndExpand">