Articles in this section

How to validate an ip address in DataForm using SfMaskedEdit in Xamarin.Android?

 

DataForm allows you to add custom editor by overriding the DataFormEditor class which is used to add custom view in DataForm.

 

This article explains adding custom SfMaskedEdit editor in DataForm, which is an advanced version of the EditText control that restricts the input to certain types of characters, text, and numbers using mask pattern.

 

ExpenseInfo:
 
   public class ExpenseInfo : INotifyPropertyChanged, INotifyDataErrorInfo
    {
 
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
 
        private string balance = "100";
        public string Balance
        {
            get
            {
                return balance;
            }
            set
            {
                balance = value;
                RaisePropertyChanged("Balance");
            }
        }
        private string _ItemName = "Education";
        public string ItemName
        {
            get
            {
                return _ItemName;
            }
            set
            {
                _ItemName = value;
            }
        }
 
        [Display(AutoGenerateField = false)]
        public bool HasErrors
        {
            get
            {
                return false;
            }
        }
 
        public IEnumerable GetErrors(string propertyName)
        {
            var list = new List<string>();
            if (!propertyName.Equals("Balance"))
                return list;
            else
            {
            }
            return list;
        }
    }

 

By passing custom view in the DataFormEditor class, you can add custom editor in data form. You can refer to the DataForm user guide documentation for creating a new custom editor: https://help.syncfusion.com/xamarin-android/sfdataform/editing#creating-new-custom-editor

 

The custom view (MaskedEdit) property settings, commit, and data validation can be handled by overriding required methods in the DataFormEditor class

 

Manually commit the custom DataFormItem editor value by using the OnCommitValue  override method of DataFormEditor class on the custom editor ValueChanged event which is used to update the custom editor value in the DataObject property of DataForm.

 

You can validate the masked edit data by implementing the INotifyDataErrorInfo interface in the data object class. Manually validate the custom editor value by using the OnValidateValue  override method of DataFormEditor class on the custom editor ValueChanged event which is used to validate the custom editor value in DataForm.

 

 
   public class CustomMaskEditor : DataFormEditor<SfMaskedEdit>
    {
        Context context;
        public CustomMaskEditor(SfDataForm dataForm,Context _context) : base(dataForm)
        {
            context = _context;
        }
 
        protected override SfMaskedEdit OnCreateEditorView()
        {
            return new SfMaskedEdit(context);
        }
        protected override void OnInitializeView(DataFormItem dataFormItem, SfMaskedEdit view)
        {
            view.Mask = @"000\.000\.000\.000";
            view.Value =(string)this.DataForm.ItemManager.GetValue(dataFormItem);
        }
        protected override void OnWireEvents(SfMaskedEdit view)
        {
            view.ValueChanged += View_ValueChanged;
        }
 
        void View_ValueChanged(object sender, ValueChangedEventArgs eventArgs)
        {
            OnCommitValue(sender as SfMaskedEdit);
            OnValidateValue(sender as SfMaskedEdit);
        }
 
        protected override void OnUnWireEvents(SfMaskedEdit view)
        {
            view.ValueChanged -= View_ValueChanged;
        }
        protected override bool OnValidateValue(SfMaskedEdit view)
        {
           return this.DataForm.Validate("Balance");
        }
        protected override void OnCommitValue(SfMaskedEdit view)
        {
            var dataFormItemView = view.Parent as DataFormItemView;
            this.DataForm.ItemManager.SetValue(dataFormItemView.DataFormItem, view.Value);
        }
    } 

 

 

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

 

 
      dataForm.DataObject = new ExpenseInfo();
      dataForm.RegisterEditor("CustomMaskEditors", new CustomMaskEditor(dataForm,this));
      dataForm.RegisterEditor("Balance", "CustomMaskEditors");

 

To download the sample click MaskedTextEditor.

 

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