Articles in this section
Category / Section

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

1 min read

 

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

 

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

 

ExpenseInfo.cs
 
    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 _ItemName = "Education";
        public string ItemName
        {
            get
            {
                return _ItemName;
            }
            set
            {
                _ItemName = value;
            }
        }
 
        private string balance = "100";
        public string Balance
        {
            get
            {
                return balance;
            }
            set
            {
                balance = value;
                RaisePropertyChanged("Balance");
            }
        }
 
        [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 DataForm. You can refer to the DataForm user guide documentation for creating a new custom editor:

https://help.syncfusion.com/xamarin-ios/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>
    {
        public CustomMaskEditor(SfDataForm dataForm) : base(dataForm)
        {
        }
 
        protected override SfMaskedEdit OnCreateEditorView()
        {
            return new SfMaskedEdit();
        }
        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)
        {
            OnValidateValue(sender as SfMaskedEdit);
            OnCommitValue(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.Superview as DataFormItemView;
            DataForm.ItemManager.SetValue(dataFormItemView.DataFormItem, view.Value);
        }
    }
 

 

Refer to the following code example for binding DataObject and adding custom editor as CustomMaskEditor 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)
Please  to leave a comment
Access denied
Access denied