Show error message underneath Entry

jenova1628jenova1628 Member ✭✭
edited December 2018 in Xamarin.Forms

Hi I am adding validation to my entry using this tutorial https://medium.freecodecamp.org/declarative-xamarin-form-validation-c174d2a74618

It is working perfectly but I want the message to be under the Entry at the minute it is on the right side

the code to show the message is:

    `public void ShowError(View view, string message)
            {
                StackLayout layout = view.Parent as StackLayout;
                int viewIndex = layout.Children.IndexOf(view);

                if (viewIndex + 1 < layout.Children.Count)
                {
                    View sibling = layout.Children[viewIndex + 1];
                    string siblingStyleId = view.Id.ToString();
                    // Reuse the existing label
                    if (sibling.StyleId == siblingStyleId)
                    {
                        Label errorLabel = sibling as Label;
                        errorLabel.Text = message;
                        errorLabel.IsVisible = true;

                        return;
                    }
                }
                // Add new label if none exists
                layout.Children.Insert(viewIndex + 1, new Label
                {
                    Text = message,
                    FontSize = 10,
                    StyleId = view.Id.ToString(),
                    TextColor = Color.Red
                });
            }`

I believe this is happening because my stack layout in XAML that the entry is apart of is set to horizontal. Does anybody know how to give the message its own stacklayout in the above code?

Thanks

Tagged:

Posts

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    If I were doing it similar to how you seem to be trying, I would setup my own composite control to replace Entry that is a vertical StackLayout with an Entry and an error message on it.

    Personally, if you don't support UWP, I would just use https://github.com/XamFormsExtended/Xfx.Controls for a good implementation with floating labels as well.

  • jenova1628jenova1628 Member ✭✭
    edited December 2018

    @AdamMeaney Yes Adam this is only for Android, I want to avoid using packages for this, also i want to reuse this for my pickers but i see that xfx does not have a picker yet

  • jenova1628jenova1628 Member ✭✭

    @NMackay This does help me unfortunately

  • NMackayNMackay GBInsider, University mod

    @jenova1628 said:
    @NMackay This does help me unfortunately

    I don't get what your asking, Forms just surfaces the native picker implementation (ComboBox for example in UWP)
    https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.picker?view=xamarin-forms

  • JohnHardmanJohnHardman GBUniversity mod

    @jenova1628 said:
    @AdamMeaney Yes Adam this is only for Android, I want to avoid using packages for this, also i want to reuse this for my pickers but i see that xfx does not have a picker yet

    Same principle as per the composite control mentioned by @AdamMeaney for having Entry with a validation message. However, you would normally only have valid options in a Picker, with the options being updated if the collection of valid possibilities changes, so there would normally not be a requirement to put a validation message under a Picker.

  • NMackayNMackay GBInsider, University mod

    @JohnHardman said:

    @jenova1628 said:
    @AdamMeaney Yes Adam this is only for Android, I want to avoid using packages for this, also i want to reuse this for my pickers but i see that xfx does not have a picker yet

    Same principle as per the composite control mentioned by @AdamMeaney for having Entry with a validation message. However, you would normally only have valid options in a Picker, with the options being updated if the collection of valid possibilities changes, so there would normally not be a requirement to put a validation message under a Picker.

    We actually had to do this in the last sprint and we used Adam's approach, also used RadControls which has a nice RadBorder to show a red border for required (even with the picker) or blue border when focused, you could achieve this with a frame and trigger to though I reckon. We used VisualStateManager to good effect to handle the border changes.

  • JohnHardmanJohnHardman GBUniversity mod
    edited December 2018

    @NMackay said:
    We actually had to do this in the last sprint and we used Adam's approach, also used RadControls which has a nice RadBorder to show a red border for required (even with the picker) or blue border when focused, you could achieve this with a frame and trigger to though I reckon. We used VisualStateManager to good effect to handle the border changes.

    Good point - mandatory selection validation would be a scenario where it could make sense to have a message underneath a Picker, possibly in association with a border color.

Sign In or Register to comment.