×

Notice

The forum is in read only mode.
×
Getting new Releases

To get notified on new releases you may follow us on twitter @expandframework or subscribe in our main RSS feed.

Topic-icon Setting editor's MaxLength according to IModelMember.Size

  • ömer ali
  • ömer ali's Avatar Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
More
4 years 3 months ago #8176 by ömer ali
I write this controller to prevent users from entering more than database column's size.
public class MaxLengthController: ViewController
    {
        DetailView DetailView = null;
        ListView ListView = null;
        protected override void OnActivated()
        {
            base.OnActivated();
            DetailView = View as DetailView;
            ListView = View as ListView;
            if (DetailView != null) DetailView.ItemsChanged += View_ItemsChanged;
        }

        protected override void OnDeactivated()
        {
            if (DetailView != null) DetailView.ItemsChanged -= View_ItemsChanged;
            base.OnDeactivated();
        }

        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            if (DetailView != null)
            {
                foreach (PropertyEditor item in DetailView.GetItems<PropertyEditor>())
                {
                    SetMaxLength(item);
                }
            }
            else if (ListView != null)
            {
                WinColumnsListEditor editor = ListView.Editor as WinColumnsListEditor;
                if (editor != null)
                {
                    foreach (GridColumn column in editor.ColumnView.Columns)
                    {
                        RepositoryItemTextEdit repository = column.ColumnEdit as RepositoryItemTextEdit;
                        if (repository == null) continue;
                        var modelColumn = column.Model();
                        if (modelColumn.MaxLength > 0) repository.MaxLength = modelColumn.MaxLength;
                    }
                }
            }
        }

        private void View_ItemsChanged(object sender, ViewItemsChangedEventArgs e)
        {
            if (e.ChangedType == ViewItemsChangedType.Added && e.Item is PropertyEditor)
            {
                SetMaxLength((PropertyEditor)e.Item);
            }
        }

        private void editor_ControlCreated(object sender, EventArgs e)
        {
            PropertyEditor editor = (PropertyEditor)sender;
            editor.ControlCreated -= editor_ControlCreated;
            SetMaxLength(editor);
        }

        private void SetMaxLength(PropertyEditor editor)
        {
            if (editor.MemberInfo.MemberType.Equals(typeof(string)) && editor.Model.MaxLength > 0)
            {
                if (editor.Control == null)
                {
                    editor.ControlCreated += editor_ControlCreated;
                }
                else if(editor.Control is TextEdit)
                {
                    TextEdit itemControl = editor.Control as TextEdit;
                    itemControl.Properties.MaxLength = editor.Model.MaxLength;
                }
            }
        }       
    }
More
4 years 3 months ago #8212 by Tolis
isn't this already supported by XAF ?

To get notified on new releases follow us on twitter @expandframework or subscribe RSS feed. I am also available @tolisss . Also see How to debug eXpand
  • ömer ali
  • ömer ali's Avatar Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
More
4 years 3 months ago #8213 by ömer ali
No, not supported by XAF.
https://documentation.devexpress.com/#Xaf/CustomDocument2701
Specifies the maximum number of characters that can be stored in a column created to store the data of the target string type property. If this attribute is not applied, a string property can be set to a value consisting of a maximum of 100 characters.

Note that this attribute only specifies the database column size. The attribute does not prevent users from entering more than the specified number of characters via Property Editors. For instance, suppose you have a string property decorated with the Size attribute that specifies that the corresponding database column's size must be 10. If a custom Property Editor used to visualize the property allows users to enter more characters, it is perfectly valid for them to do so. In this instance, a SQL exception will be raised when trying to save an object. To ensure that users will not be able to enter and save more than a fixed maximum number of characters, use the Validation Module. For example, you can decorate a business class with RuleCriteriaAttribute, and use the Len function operator to enforce the maximum length for a string property.

In XAF, the value passed as the attribute's parameter is set as a maximum length for ASPxStringPropertyEditor and Windows Forms RichTextPropertyEditor.

In fact, this attribute can be applied to any property type. This will influence the visibility of the corresponding List View column. If the and DelayedAttribute are not applied to a target property and the SizeAttribute's parameter passes a value that is more than 255 (e.g. SizeAttribute.Unlimited), the corresponding column is set invisible by default.
More
4 years 3 months ago #8236 by Tolis
from what i see your controller does what MaxLenght attribute does
documentation.devexpress.com/#Xaf/DevExp...ditor_MaxLengthtopic

please correct me if i am wrong

To get notified on new releases follow us on twitter @expandframework or subscribe RSS feed. I am also available @tolisss . Also see How to debug eXpand
  • ömer ali
  • ömer ali's Avatar Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
More
4 years 3 months ago #8237 by ömer ali
Sorry my mistake. You are right.
i noticed that It is not work my custom property editor.
Moderators: expand
Time to create page: 0.225 seconds