mirror of
https://github.com/bitwarden/mobile
synced 2025-12-05 23:53:33 +00:00
36 lines
1.2 KiB
C#
36 lines
1.2 KiB
C#
using System;
|
|
using Xamarin.Forms;
|
|
|
|
namespace Bit.App.Behaviors
|
|
{
|
|
public class RequiredValidationBehavior : Behavior<Entry>
|
|
{
|
|
private static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(RequiredValidationBehavior), false);
|
|
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
|
|
|
|
public bool IsValid
|
|
{
|
|
get { return (bool)GetValue(IsValidProperty); }
|
|
private set { SetValue(IsValidPropertyKey, value); }
|
|
}
|
|
|
|
protected override void OnAttachedTo(Entry entry)
|
|
{
|
|
entry.TextChanged += HandleTextChanged;
|
|
base.OnAttachedTo(entry);
|
|
}
|
|
|
|
void HandleTextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
IsValid = !string.IsNullOrWhiteSpace(e.NewTextValue);
|
|
((Entry)sender).BackgroundColor = IsValid ? Color.Default : Color.Red;
|
|
}
|
|
|
|
protected override void OnDetachingFrom(Entry entry)
|
|
{
|
|
entry.TextChanged -= HandleTextChanged;
|
|
base.OnDetachingFrom(entry);
|
|
}
|
|
}
|
|
}
|