Pxaml
Pxaml

Reputation: 553

editor length validation xamarin forms

I would like to do some validation to this editor , for example "editor can not be empty write something". I could validate as input < 0 , however, I will need this validation in order to go to the next page . Any ideas ? Thanks. this is my xaml.

     <StackLayout Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3">
                        <Label Text="Description" FontSize="Medium" Style="{StaticResource LabelStyle}" />
                        <Editor x:Name="Description" FontSize="Medium" HeightRequest="120" TextChanged ="Handle_TextChanged" />
<Label x:Name ="Errorlabel"/>
                    </StackLayout>

cs:

 async void Send_Activated(object sender, System.EventArgs e)
        {
            var editor = EDescription.Text;

            if (string.IsNullOrWhiteSpace (editor))
            {
                Errorlabel.Text = "Plase add a description ";
                ToolbarItems.Clear();

            }

            if (editor.Length >1)
            {
                await App.Navigator.PushAsync(new 2View());  
            }
      }

toolbar :

<ContentPage.ToolbarItems>
        <ToolbarItem  x:Name = "anySend" Text="Send" Order="Primary" Activated="Send_Activated"  />
    </ContentPage.ToolbarItems>

Upvotes: 0

Views: 724

Answers (1)

Jason
Jason

Reputation: 89092

a simple way to validate for non-null entries:

if (string.IsNullOrEmpty(Description.Text)) {
    DisplayAlert("Error","Please enter a description", "OK");
} else {
    Navigation.PushAsync(nextPage);
}

Xamarin also has an extensive writeup on doing Validation in MVVM

Upvotes: 2

Related Questions