We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to set HasError in TextInputLayout from Entry?

I've an entry for email with a behavior checking if the field is correct.
In XAML is:

<inputLayout:SfTextInputLayout ContainerType="Outlined" Hint="Indirizzo Email" ErrorText="L'indirizzo email non è valido">
    <Entry TextColor="White" Text="{Binding Email}" Keyboard="Email" IsVisible="{Binding IsNotPartner}" >
            <local:EmailValitationBehavior />

The Behavior is:

public class EmailValitationBehavior : Behavior<Entry>
        const string emailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +

        protected override void OnAttachedTo(Entry bindable)
            bindable.Unfocused += HandleCompleted;
        void HandleCompleted(object sender, EventArgs e)
            Entry s = (Entry)sender;
            bool IsValid = (Regex.IsMatch(s.Text, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
            s.TextColor = IsValid ? Color.Blue : Color.Red;
            (s.Parent as SfTextInputLayout).HasError = IsValid ? false : true;
        protected override void OnDetachingFrom(Entry bindable)
            bindable.Unfocused -= HandleCompleted;

I've tryied to set the HasError of the Parent of Entry, but its doesn't work.
How to do that in a MVVM environment (PRISM)?

Thanks a lot and have a nice day,

1 Reply

BK Bharathiraja K Syncfusion Team June 4, 2019 11:56 AM UTC

Hi Andrea, 
Thank you for your interest in Syncfusion products. 
We have prepared the sample based on your requirement. Please refer the below code snippet for your understanding. 
Code Snippet [C#]: 
private void Entry_Unfocused(object sender, FocusEventArgs e) 
Entry inputView = (Entry)sender; 
bool IsValid = (Regex.IsMatch(inputView.Text, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))); 
inputView.TextColor = IsValid ? Color.Blue : Color.Red; 
model.TextHasError = !IsValid; 
You can also achieve your requirement by using custom behavior with below change in your code snippet. 
Code Snippet [C#] 
(s.Parent.Parent.Parent as SfTextInputLayout).HasError = IsValid ? false : true; 
Please download the sample from the below link. 

Live Chat Icon For mobile
Up arrow icon