Xamarin Forms Picker Validation Help?

Hey does anybody have an idea on how to make a picker required(behavior) in Xamarin forms? meaning if the user does not select a value and try to press the submit button it comes up with an alert saying please fill in required fields?



  • JohnHardmanJohnHardman GBUniversity mod

    @jenova1628 - What have you tried so far?

  • jenova1628jenova1628 Member ✭✭

    @JohnHardman honestly not a lot I just tried simple if statements for the selected item being null that did not seem to work

  • jenova1628jenova1628 Member ✭✭


    if(VehiclePicker == null) { displayAlert }

  • JohnHardmanJohnHardman GBUniversity mod

    @jenova1628 - One line of pseudo-code really isn't showing that you've spent time on this. Others are more likely to help if they've seen you've tried first.

  • jenova1628jenova1628 Member ✭✭

    @JohnHardman my apologies for the lack of knowledge on this topic, I can't seem to come across any decent articles or samples of Picker Validation only Entry

  • JohnHardmanJohnHardman GBUniversity mod


    Things to consider:

    (1) Would DisplayActionSheet do what you want, so obviating the need for the Picker and submit Button?
    (2) If using a Picker, would you simply want to disable the submit Button if no selection has been made and enable the submit Button once a selection has been made?

    If you really want to have a Picker, submit Button and to use DisplayAlert, post the code where you create and populate your Picker, where you create your submit Button, where you call DisplayAlert, and any bindings, Commands and/or event handlers that you have so far.

  • jenova1628jenova1628 Member ✭✭
    edited November 2018

    so my current picker and submit button is currently set in Xaml as

        `<Picker x:Name="GeneTestIcePicker" WidthRequest="100" Margin="15,0,0,0" Title="Select" >
                                <x:Array Type="{x:Type x:String}">
         <Button Clicked="SubmitButton_Clicked" x:Name="SubmitButton" IsEnabled="True" Text="Submit" WidthRequest="150" BorderRadius="6" BackgroundColor="Blue" TextColor="White" ></Button>`

    and in the SubmitButton_Clicked Event handler, I have an if statement of this

    `if (GeneTestIcePicker.SelectedIndex == -1)
                    DisplayAlert("Error", "Please fill in all the required fields", "Ok");
  • JohnHardmanJohnHardman GBUniversity mod

    @jenova1628 said:
    so my current picker and submit button is currently set in Xaml as

    So, if you uncomment the clicked handler code, what happens?

    You'll probably want to await DisplayAlert and you might want to use a Command on the Submit button instead of the clicked handler,

  • jenova1628jenova1628 Member ✭✭

    Sorry, The code is not commented I think I uploaded it to the discussion incorrectly. It is currently working so if I leave it blank and press Submit it displays the alert and if I chose a selection it submits the data. but I am not sure this is the best way to do this because I will be having multiple pickers on the page and that would mean loads of else if's doing it this way right?

  • JohnHardmanJohnHardman GBUniversity mod

    @jenova1628 said:
    It is currently working

    If you have something working but want to know best practice, you need to state that up front rather than give the impression that you need help getting it to work at all. I'm stepping out of this thread now.

Sign In or Register to comment.