SfDataForm Password Editor Example

Hi,

I am looking for an example to make an Entry editor to support Password.
Is there such project example for that? If not, how do I change a dataformitem to support Password field?
Thanks.

rgds,
CY

1 Reply

JM Jeyasri M Syncfusion Team June 26, 2018 11:57 AM UTC

Hi Liau,  
 
Thanks for contacting Syncfusion Support.  
 
We have included the in-built Password Editor in SfDataForm by defining DataType attribute as Password in our Essential Studio Volume 2, 2018 (Version 16.2.0.41) and is available for download under the following link. 
 
 
Please refer the in-built editors in SfDataForm. 
 
Please find the below code example to define PasswordEditor in data form. 
[C#] 
 
        [Display(Prompt ="In built password editor")] 
        [Required(AllowEmptyStrings = false, ErrorMessage = "Value should not be empty")] 
        [DataType(DataType.Password)] 
        public string Captcha { get; set; } 
 
 
Using Custom Editor support: 
 
And Also you can add the custom Password editor DataFormEditor class which is used add new editor in SfDataForm. In this example we have added custom editor with Entry as Password editor and inbuilt Password editor. 
 
Note: 
While registering the custom editor in SfDataForm, you should manually validate the custom DataFormItem editor value by using the OnValidateValue override method of DataFormEditor class on respective editor view get Unfocused. 
 
Please find the code example. 
 
[C#] 
 
public class CustomPasswordEntry : DataFormEditor<Entry> 
    { 
        public CustomPasswordEntry(SfDataForm dataForm) : base(dataForm) 
        { 
        } 
        protected override Entry OnCreateEditorView() 
        { 
            return new Entry(); 
        } 
        protected override void OnInitializeView(DataFormItem dataFormItem, Entry view) 
        { 
            view.IsPassword = true; 
        } 
 
        protected override void OnWireEvents(Entry view) 
        { 
            view.TextChanged += View_TextChanged; 
            view.Unfocused += View_Unfocused; 
        } 
 
        protected override bool OnValidateValue(Entry view) 
        { 
            var dataFormItemView = view.Parent as DataFormItemView; 
            return (DependencyService.Get<IDataForm>().ValidateDataFormItem(DataForm)); 
        } 
        private void View_Unfocused(object sender, FocusEventArgs e) 
        { 
            OnValidateValue(sender as Entry); 
        } 
 
        private void View_TextChanged(object sender, TextChangedEventArgs e) 
        { 
            OnCommitValue(sender as Entry); 
        } 
 
        protected override void OnCommitValue(Entry view) 
        { 
            var dataFormItemView = view.Parent as DataFormItemView; 
            var textValue = view.Text; 
            this.DataForm.ItemManager.SetValue(dataFormItemView.DataFormItem, view.Text); 
        } 
    } 
 
 In order to update the DataObject values when using custom editor, you should update the  DataForm Item editor value manually by using OnCommitValue method of DataFormEditor class which is used to commit the customized value in DataObject.  
 
We have prepared a sample based on your requirement SfDataForm Password Editor added and please find the same from below link. 
 
Sample: 
PasswordEditor
 
 
 
Note: 
In the above sample we have manually did the validation for custom editor in ValidateDataFormItem  
internal method of native renderer of DataForm ItemManager class by using Reflection.  
 
Code example: 
[C#] 
 
[assembly: Xamarin.Forms.Dependency(typeof(DataFormValidation))] 
 
    public class DataFormValidation : IDataForm 
    { 
        public bool ValidateDataFormItem(SfDataForm dataForm) 
        { 
           var nativeObject =  dataForm.GetType().GetProperty("NativeObject", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(dataForm); 
 
            if (nativeObject != null) 
            { 
                var nativeDataForm = (nativeObject as Syncfusion.Android.DataForm.SfDataForm); 
                var method = nativeDataForm.ItemManager.GetType().GetMethod("ValidateDataFormItem", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); 
                var nativedataFormItem = nativeDataForm.ItemManager.DataFormItems["Password"]; 
                if (nativedataFormItem != null && method != null) 
                { 
                    var nativeValue = nativeDataForm.ItemManager.GetValue(nativedataFormItem); 
                    method.Invoke(nativeDataForm.ItemManager, new object[] { nativedataFormItem, nativeValue, true }); 
                    var isValid = nativedataFormItem.GetType().GetProperty("IsValid", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(nativedataFormItem); 
                    return (bool)isValid; 
                } 
            } 
            return true; 
        } 
    } 
 
Regards, 
Jeyasri M 


Loader.
Up arrow icon