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
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
We are researching the usage of ForceValidateCommand, please give us more time.
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:
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.
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
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.
The problem is that I have a custom control where I implement validation, it sounds complicated.
9 people are following this question.