Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

ValidationBehavior: Added IsNotValid property #836

Merged
merged 5 commits into from
Feb 10, 2021
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,11 +15,21 @@
Spacing="50"
VerticalOptions="CenterAndExpand">
<Label Text="Text color will change accordingly to the style that is configured when a invalid value (email address) is entered." />
<Entry Placeholder="Email">
<Entry.Behaviors>
<xct:EmailValidationBehavior DecorationFlags="Trim" InvalidStyle="{StaticResource InvalidEntryStyle}"/>
</Entry.Behaviors>
</Entry>
<StackLayout Spacing="3">
<Entry Placeholder="Email">
<Entry.Behaviors>
<xct:EmailValidationBehavior
x:Name="EmailValidator"
DecorationFlags="Trim"
InvalidStyle="{StaticResource InvalidEntryStyle}"/>
</Entry.Behaviors>
</Entry>
<Label Text="Invalid email"
TextColor="Red"
FontSize="10"
IsVisible="{Binding IsNotValid, Source={x:Reference EmailValidator}}"
/>
</StackLayout>
</StackLayout>

</pages:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ namespace Xamarin.CommunityToolkit.Behaviors.Internals
{
public abstract class ValidationBehavior : BaseBehavior<VisualElement>
{
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down