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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set HasError in TextInputLayout from Entry?

Thread ID:

Created:

Updated:

Platform:

Replies:

145011 Jun 1,2019 02:30 PM UTC Jun 4,2019 11:56 AM UTC Xamarin.Forms 1
loading
Tags: SfTextInputLayout
Andrea Ferrari
Asked On June 1, 2019 02:30 PM UTC

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
 

Bharathiraja K [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon