question

Angelru-4290 avatar image
0 Votes"
Angelru-4290 asked denledphilips edited

ForceValidateCommand

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

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

We are researching the usage of ForceValidateCommand, please give us more time.


0 Votes 0 ·

I didn't know that research part, I'll have to wait.

0 Votes 0 ·
LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered NicoleLu-9366 commented

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.


· 12
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Very well explained, but there is something that I do not understand, you are using the click event, but for example: I have a binding command to my ViewModel to make a login, I have no way of knowing if it is valid or not, since ForceValidate is called from the codebehind

0 Votes 0 ·

If you do not execute the ForceValidate method in background code, you have to create a custom ValidationBehavior, add your logic to execute ForceValidateCommand, with Command.Execute() method.

0 Votes 0 ·

The problem is that I have a custom control where I implement validation, it sounds complicated.

0 Votes 0 ·
Show more comments
denledphilips avatar image
0 Votes"
denledphilips answered denledphilips edited
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.