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?

Hi,
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}" >
        <Entry.Behaviors>
            <local:EmailValitationBehavior />
        </Entry.Behaviors>
    </Entry>
</inputLayout:SfTextInputLayout>

The Behavior is:

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

        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.Unfocused += HandleCompleted;
            base.OnAttachedTo(bindable);
        }
        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;
            base.OnDetachingFrom(bindable);
        }
    }

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,
Andrea
 

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; 
} 
 
Note: 
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. 
 
 
Regards, 
Bharathi. 


Loader.
Live Chat Icon For mobile
Up arrow icon