ForceValidateCommand

Ángel Rubén Valdeolmos Carmona 586 Reputation points
2021-06-02T16:11:51.287+00:00

I am using the behaviors from https://github.com/xamarin/XamarinCommunityToolkit

How can I force validation from a command? I don't know how to use ForceValidateCommand

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,337 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,641 Reputation points Microsoft Vendor
    2021-06-04T05:18:10.87+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    We didn't find details about the ForceValidateCommand usage in documentation. The community toolkit documentation is still under construction, any suggestions are welcomed, you can put a request about the ForceValidateCommand usage in the github repo.

    https://github.com/xamarin/XamarinCommunityToolkit

    And here's some findings from our side for your reference:

    By checking the source code for ValidationBehavior class in github: https://github.com/xamarin/XamarinCommunityToolkit/blob/659a582475c4ea1fd4a5f8c6ef5cfe4b7b323542/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/ValidationBehavior.shared.cs#L11

    See following Comment

    The <see cref="ValidationBehavior"/> allows users to create custom validation behaviors. All of the validation behaviors in the Xamarin Community Toolkit inherit from this behavior, to expose a number of shared properties. Users can inherit from this class to create a custom validation behavior currently not supported through the Xamarin Community Toolkit. This behavios cannot be used directly as it's abstract

    This is a general class which has some default behaviors and we need to inherit from it to create more customized behaviors. Take CharactersValidationBehavior for example, this is a real implementation. But it did not customize the ForceValidateCommand, it just used ValidationBehavior's default command which is called DefaultForceValidateCommand, details can be found in source code:

    https://github.com/xamarin/XamarinCommunityToolkit/blob/659a582475c4ea1fd4a5f8c6ef5cfe4b7b323542/src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/Validators/ValidationBehavior.shared.cs#L72

    And the DefaultForceValidateCommand is calling ForceValidate() method which will then make a call to UpdateStateAsync() method. We didn't find anywhere that calls the command to execute, so this may need your customization to execute the command, anyway we can still check how the command behave by explicitly calling ForceValidate() method:

    Here is an example to make a test. If you have a xct:CharactersValidationBehavior,

    <Entry x:Name="myEntry" Placeholder="Type characters..." >
                <Entry.Behaviors>
                    <xct:CharactersValidationBehavior
    Flags="ValidateOnValueChanging"
    ForceValidateCommand="{Binding MyValidateCommand}"
    
    MaximumCharacterCount="5"
    MinimumCharacterCount="1"/>
                </Entry.Behaviors>
    
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Valid">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="Green"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Invalid">
                            <VisualState.Setters>
                                <Setter Property="TextColor" Value="IndianRed"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Entry>
    

    If this entry is empty, we get the result by IsNotValid method. this res1's value is false, when I click the button, to execute haractersValidationBehavior.ForceValidate();, then use IsNotValid method, this res's value is true.

    public MainPage()
            {
                InitializeComponent();
    
    
    
                BindingContext = new MyViewModel(this);
    
                charactersValidationBehavior = myEntry.Behaviors[0] as CharactersValidationBehavior;
                var res1= charactersValidationBehavior.IsNotValid;
    
            }
    
            private void Button_Clicked(object sender, EventArgs e)
            {
    
               charactersValidationBehavior.ForceValidate();
                var res = charactersValidationBehavior.IsNotValid;           
            }
        }
    

    Anyway, it's suggested to file an request for detail documentation about the ForceValidateCommand usage in github.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Đèn led philips 1 Reputation point
    2021-06-29T03:45:31.077+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.