Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] RequiredStringValidationBehavior stays invalid when it should be valid #1969

Open
CostasAthan opened this issue May 8, 2023 · 7 comments
Labels
bug Something isn't working. Breaky break.

Comments

@CostasAthan
Copy link

Description

RequiredStringValidationBehavior stays always invalid

Steps to Reproduce

StackLayout myLayout = new StackLayout
{ 
    Children =
    {
        new Entry(),

        new Entry(),
                   
        new Entry()
    }
};

myLayout.Children.OfType<Entry>().ElementAt(2).Behaviors.Add
(
     new RequiredStringValidationBehavior
      {
           RequiredString = myLayout.Children.OfType<Entry>().ElementAt(1).Text,
           InvalidStyle = invalidEntryStyle,
           Flags = ValidationFlags.ValidateOnValueChanging
      }
);

Expected Behavior

  1. Run the aforementioned code.
  2. Enter the same strings in both the second and the third Entries.

Actual Behavior

The RequiredStringValidationBehavior stays invalid

Basic Information

  • Version with issue: XamarinCommunityToolkit 2.0.6
@CostasAthan CostasAthan added the bug Something isn't working. Breaky break. label May 8, 2023
@FedericoNembrini
Copy link
Contributor

I think you are not properly binding RequiredString.

At the moment you are setting RequiredString with the current value of myLayout.Children.OfType<Entry>().ElementAt(1).Text.

You should do something like this:

requiredStringValidationBehaviorReference
.SetBinding(
    RequiredStringValidationBehavior.RequiredStringProperty,
    new Binding("Text", source: myLayout.Children.OfType<Entry>().ElementAt(1))
);

For reference: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings#bindings-without-a-binding-context

@CostasAthan
Copy link
Author

At the moment you are setting RequiredString with the current value of myLayout.Children.OfType().ElementAt(1).Text

Yes. Isn't RequiredString the string that will be compared to the value provided in the third entry? When both the second and the third entries have as a value exactly the same string, shouldn't the above code return a valid state for the third one?

@FedericoNembrini
Copy link
Contributor

FedericoNembrini commented May 10, 2023

When you are executing this?

myLayout.Children.OfType<Entry>().ElementAt(2).Behaviors.Add
(
     new RequiredStringValidationBehavior
      {
           RequiredString = myLayout.Children.OfType<Entry>().ElementAt(1).Text,
           InvalidStyle = invalidEntryStyle,
           Flags = ValidationFlags.ValidateOnValueChanging
      }
);

myLayout.Children.OfType<Entry>().ElementAt(1).Text is already set with the desired value? Otherwise, you are setting RequiredString to string.Empty, and since you are not binding the value, even if you change the text of the second entry, RequiredString will remain string.Empty.

@CostasAthan
Copy link
Author

@FedericoNembrini

Yes, I got what you are saying!

Should the following version work though?

myLayout.Children.OfType<Entry>().ElementAt(2).Behaviors.Add
(
     new RequiredStringValidationBehavior
      {
           RequiredString = myLayout.Children.OfType<Entry>().ElementAt(1).Text,
           InvalidStyle = invalidEntryStyle,
           Flags = ValidationFlags.ValidateOnValueChanging,
           BindingContext = myLayout.Children.OfType<Entry>().ElementAt(1).Text
      }
)

It doesn't work either.

@FedericoNembrini
Copy link
Contributor

No, it cannot work. You have to set the binding with .SetBinding()

@CostasAthan
Copy link
Author

What's the purpose of BindingContext property then?

@FedericoNembrini
Copy link
Contributor

You need to learn more about Binding and BindingContext.
In simple terms the BindingContext is the object it is associated with. However, the fact remains that to tell RequiredString what property to "Bind" to, you must use SetBinding().

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working. Breaky break.
Projects
None yet
Development

No branches or pull requests

2 participants