Restrict Editing of Field Contents

4 posts, 0 answers
  1. William
    William avatar
    6 posts
    Member since:
    Mar 2015

    Posted 04 Apr 2015 Link to this post

    Hi,

    How can I restrict the user from editing a field content after he inserted it? I need to allow the user to either insert or delete fields, but not change their inner content.

    Thanks in advance.

  2. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 07 Apr 2015 Link to this post

    Hello William,

    By default the Fields are special document elements, which have the ability to update themselves automatically and editing such fields is not a very common scenario. Could you, please, provide us more information about the scenario? Why you need to restrict the user to edit and what exactly kind of Fields you are using?

    Regards,
    Tanya
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. DevCraft banner
  4. William
    William avatar
    6 posts
    Member since:
    Mar 2015

    Posted 07 Apr 2015 in reply to Tanya Link to this post

    What I mean is, once the user inserts the field, the editor by default allows him to change the field code. What I want is to not allow the user to do that, since he may invalidate the field by typing an invalid code.

    Example Scenario to avoid:

    1. The user inserts a MergeField

    2. The document shows the Mergefield in Code view as {MERGEFIELD MyProperty}

    3. The user puts the caret inside the field and changes the content from MERGEFIELD to MERGEFIELD1 by accident.

    4. The field is now invalid since there is no code as MERGEFIELD1, and the editor displays the following message in Results View: Not supported field expression!.

     

  5. Tanya
    Admin
    Tanya avatar
    402 posts

    Posted 10 Apr 2015 Link to this post

    Hi William,

    The ReadOnlyRanges are a convenient way to prevent modifications of some parts of the document. You could use them to restrict the changes for the fields. More information about this feature you can find in the Read Only Ranges article in our documentation.

    The protection, however, does not allow deleting the protected content. To fit your requirements, you could subscribe to the PreviewEditorKeyDown event of RadRichTextBox and check the pressed key. If the key is Delete/Backspace, check the positions of the Selection - the start position of the Selection should be smaller than the read only range's start position; the end position of the Selection should be bigger than the one of the range end. Please, refer to the snippet below for more details about the implementation:
    void editor_PreviewEditorKeyDown(object sender, PreviewEditorKeyEventArgs e)
    {
        if (e.Key == Key.Delete || e.Key == Key.Back)
        {
            var ranges = this.editor.Document.Selection.GetAnnotationMarkersOfType<ReadOnlyRangeStart>();
            foreach (var rangeStart in ranges.ToList())
            {
                DocumentPosition start = new DocumentPosition(this.editor.Document);
                start.MoveToInline(rangeStart);
     
                if (this.editor.Document.Selection.Ranges.First.StartPosition < start)
                {
                    DocumentPosition end = new DocumentPosition(this.editor.Document);
                    start.MoveToInline(rangeStart.End);
     
                    if (this.editor.Document.Selection.Ranges.First.EndPosition > end)
                    {
                        RadDocumentEditor toEdit = new RadDocumentEditor(this.editor.Document);
     
                        toEdit.RespectReadOnlyRanges = false;
                        toEdit.RespectNonDeletableRanges = false;
     
                        toEdit.Delete(true);
                        e.SuppressDefaultAction = true;
                    }
                }
            }
        }
    }

    You should be aware, that using this approach, you will not be able to delete the protected range if the Selection's start/end position matches the range's start end/position. This behavior comes from the SkipPositionBefore of the ReadOnlyRange and we do not have much to do in this case.

    Hope this helps.

    Regards,
    Tanya
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top