We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Retrieve errors from the form and bind to a button

Hello,
I'm using SfDataForm with MVVM pattern and  with a custom class bound as source.
I'm using also it with validationmode.lostfocus.

I'd like to bind the IsEnabled property of a button to the errors count of the sfdataform (no errors means the button is enabled), but I do not understand how to get errors dinamically from the object in my viewmodel.

How can I achieve this?
Thanks,
Davide

6 Replies

SP Subburaj Pandian Veluchamy Syncfusion Team August 12, 2019 07:49 AM UTC

Hi Davide, 
  
Thank you for contacting Syncfusion support. 
  
We have analyzed your requirement of “Get validation error count in DataForm”.Validation error count can get from ValidationCompletedEvent if you are using DataFormExplicitValidation. Since you are using LostFocus ValidationMode, your requirement can be achieved using DataForm Validated event. ValidatedEvent will be triggered after each DataFormItem validated, in which you can check for whether all fields are valid usingIsValid property of DataFormItem class. 
  
Code Snippet: 
        
private void DataForm_Validated(object sender, ValidatedEventArgs e) 
{ 
          button.IsEnabled = (sender as SfDataForm).ItemManager.DataFormItems.TrueForAll(x => (x asDataFormItem).IsValid); 
  } 
  
We have prepared a simple sample for the same, 
 
Sample link: DataForm 
  
We hope this helps. Kindly revert us if you have any concern. 
 
Regards,
Subburaj Pandian V   



DD Davide D Angelo August 16, 2019 01:24 PM UTC

Thank you Subburaj,
your answer has solved the issue, but after implemented it, I decided to change the validation mode to "Explicit".
I'd like to validate the form (sfdataform) in my viewmodel, when a button with command has been clicked: how can I achieve this? I'd like to avoid to call validate method of sfdataform in my page.xaml.cs page.


Also, as second question, is there any option to validate the entries in the form in the lost focus and also call the validation mode Explicit?
Regards,
Davide


GC Ganeshamoorthy Chandramoorthy Syncfusion Team August 19, 2019 02:12 PM UTC

Hi Davide, 

 

Thanks for the update.

 

Currently we are analyzing on your query, we will analyze and update you the further details in two business days (August 21, 2019). We appreciate your patience until then.  

 

Regards,
Ganeshamoorthy C 



SP Subburaj Pandian Veluchamy Syncfusion Team August 21, 2019 11:01 AM UTC

Hi Davide, 
  
Thank you for your patience. 
  
Query 1: Regarding validation using Button command in ViewModel 
DataForm can be validated in ViewModel using Button Command. While button has clicked pass DataForm instance as Command parameter and validate the DataForm in ViewModel using DataForm Validate method. 
  
[Code Snippet] 
[XAML] 
<dataForm:SfDataForm x:Name="dataForm" DataObject="{Binding LoginInfo}" Grid.Row="0"ValidationMode="LostFocus" CommitMode="LostFocus"/> 
  
<Button x:Name="validateButton" Grid.Row="1"  Text="Submit" 
    Command="{Binding Path=BindingContext.ValidateCommand, Source={x:Reference Name=dataForm}}" CommandParameter="{Binding Source={x:Reference Name=dataForm}}"/> 
  
[C#]        
public DataFormViewModel() 
{ 
     loginInfo = new DataFormModel(); 
     this.ValidateCommand = new Command<object>(this.OnValidate); 
} 
  
public Command<object> ValidateCommand { getset; } 
  
private void OnValidate(object dataForm) 
{ 
       var dataFormLayout = dataForm as Syncfusion.XForms.DataForm.SfDataForm; 
       dataFormLayout.Validate(); 
  } 
  
Query 2: Regarding validating DataFormItems in both LostFocus and Explicit validation 
DataFormItems can be validated explicitly even if ValidationMode set to LostFocus or PropertyChanged but If ValidationMode set to Explicit, then explicit validation only can be done. So, set ValidationMode to LostFocus and call explicit validation using Validate() method both will work fine with DataForm. 
  
We have prepared a simple sample including solutions for both queries,         
 
Sample link: DataForm 
  
We hope this helps. Kindly revert us if you have any concern. 
  
Regards,
Subburaj Pandian V  



DD Davide D Angelo September 2, 2019 09:47 AM UTC

Hi Subburaj,
that was what I needed.
It works
Thank you,
Davide 


SP Subburaj Pandian Veluchamy Syncfusion Team September 3, 2019 07:10 AM UTC

Hi Davide,  
   
Thank you for the update. We are glad that the given solution meets your requirement.

Please get in touch with us, if you would require any further assistance.

 
Regards,
Subburaj Pandian V  


Loader.
Live Chat Icon For mobile
Up arrow icon