Articles in this section
Category / Section

How to commit and validate the custom editor value to corresponding DataObject property in Xamarin.Forms SfDataForm?

1 min read

DataForm allows you to commit and validate the custom editor values. This article explains handling custom entry editor in Xamarin Forms DataForm.

 

Here, the Age data field is loaded as custom entry editor.

 

ContacForm.cs
 
     public class ContactForm : INotifyPropertyChanged
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
 
        private int age;
        [Required(AllowEmptyStrings = false, ErrorMessage = "Age should not be empty")]
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
                RaisePropertyChanged("Age");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        private void RaisePropertyChanged(String Name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
        }
    }
 

 

By passing custom view in DataFormEditor class, you can add custom editor in data form. Refer to this DataForm user guide documentation for creating a new custom editor.

 

 
public class CustomTextEditor : DataFormEditor<Entry>
    {
        public CustomTextEditor(SfDataForm dataForm) : base(dataForm)
        {
        }
 
        protected override Entry OnCreateEditorView(DataFormItem dataFormItem)
        {
            return new Entry();
        }
        protected override void OnInitializeView(DataFormItem dataFormItem, Entry view)
        {
            base.OnInitializeView(dataFormItem, view);
            view.Keyboard = Keyboard.Numeric;
        }
 
        protected override void OnWireEvents(Entry view)
        {
            view.TextChanged += OnViewTextChanged;
            view.Focused += OnViewFocused;
            view.Unfocused += OnViewUnfocused;
        }
 
        private void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnValidateValue(sender as Entry);
        }
 
        private void OnViewFocused(object sender, FocusEventArgs e)
        {
            var view = (sender as Entry);
            view.TextColor = Color.Green;
        }
 
        protected override bool OnValidateValue(Entry view)
        {
            return this.DataForm.Validate("Age");
        }
        private void OnViewUnfocused(object sender, FocusEventArgs e)
        {
            var view = sender as Entry;
            view.TextColor = Color.Red;
 
            if (this.DataForm.CommitMode == Syncfusion.XForms.DataForm.CommitMode.LostFocus || this.DataForm.ValidationMode == ValidationMode.LostFocus)
                this.OnValidateValue(view);
            if (this.DataForm.CommitMode != Syncfusion.XForms.DataForm.CommitMode.LostFocus) return;
            this.OnCommitValue(view);
            OnValidateValue(sender as Entry);
        }
        private void OnViewTextChanged(object sender, TextChangedEventArgs e)
        {
            var view = sender as Entry;
            if (DataForm.CommitMode == Syncfusion.XForms.DataForm.CommitMode.PropertyChanged || DataForm.ValidationMode == ValidationMode.PropertyChanged)
                this.OnValidateValue(view);
            if (this.DataForm.CommitMode != Syncfusion.XForms.DataForm.CommitMode.PropertyChanged) return;
            this.OnCommitValue(view);
        }
 
        protected override void OnCommitValue(Entry view)
        {
            var dataFormItemView = view.Parent as DataFormItemView;
            this.DataForm.ItemManager.SetValue(dataFormItemView.DataFormItem, view.Text);
        }
 
        protected override void OnUnWireEvents(Entry view)
        {
            view.TextChanged -= OnViewTextChanged;
            view.Focused -= OnViewFocused;
            view.Unfocused -= OnViewUnfocused;
        }
    }

 

You should manually commit the custom DataFormItem editor value by using OnCommitValue override method of DataFormEditor class on custom editor Value or Focus changed event which is used to update the custom editor value in respective property in DataObject based on dataform commit mode set.

 

Also , you should manually validate the custom editor value in by using OnValidateValue override method of DataFormEditor class on custom editor Value or Focus changed event which is used to validate the custom editor value based on data form validation mode set . In the override method for OnValidateValue, you need to return DataForm.Validate(string) method in order to validate the particular data item.

 

Refer to the following code example for binding DataObject and adding custom editor(Entry) as CustomEditor using the RegisterEditor method in DataForm.

 

 
            dataForm.RegisterEditor("numeric", new CustomTextEditor(dataForm));
            dataForm.RegisterEditor("Age", "numeric");
            dataForm.ValidationMode = ValidationMode.LostFocus;
            dataForm.DataObject = new ContactForm();

 

 

Sample Demo: HandlingCustomEditorValue

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied