Last active
August 25, 2018 20:28
-
-
Save ameerthehacker/d900795c4ef4f1b18f1bafe1d755688e to your computer and use it in GitHub Desktop.
Implementation of IErrorStyle for basic errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xamarin.Forms; | |
using XamarinFormValidation.Validators.Contracts; | |
namespace XamarinFormValidation.Validators.Implementations | |
{ | |
public class BasicErrorStyle: IErrorStyle | |
{ | |
public void ShowError(View view, string message) | |
{ | |
StackLayout layout = view.Parent as StackLayout; | |
int viewIndex = layout.Children.IndexOf(view); | |
if(viewIndex + 1 < layout.Children.Count) { | |
View sibling = layout.Children[viewIndex + 1]; | |
string siblingStyleId = view.Id.ToString(); | |
// Reuse the existing label | |
if (sibling.StyleId == siblingStyleId) | |
{ | |
Label errorLabel = sibling as Label; | |
errorLabel.Text = message; | |
errorLabel.IsVisible = true; | |
return; | |
} | |
} | |
// Add new label if none exists | |
layout.Children.Insert(viewIndex + 1, new Label | |
{ | |
Text = message, | |
FontSize = 10, | |
StyleId = view.Id.ToString(), | |
TextColor = Color.Red | |
}); | |
} | |
public void RemoveError(View view) | |
{ | |
StackLayout layout = view.Parent as StackLayout; | |
int viewIndex = layout.Children.IndexOf(view); | |
if (viewIndex + 1 < layout.Children.Count) | |
{ | |
View sibling = layout.Children[viewIndex + 1]; | |
string siblingStyleId = view.Id.ToString(); | |
if (sibling.StyleId == siblingStyleId) | |
{ | |
sibling.IsVisible = false; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment