Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andres Cano 22 posts 42 karma points
    Apr 22, 2011 @ 17:36
    Andres Cano
    0

    Label without text custom field and text without label custom field... for nice forms

    Hi i have been looking in the forum to get examples of the code to use or to put in contour, and make custom fieldtypes for a:

    Label without text (just the label) fieldstype

    and

    Text without the Label (like a paragraph to explain how to fill a section in the form)

    but i dont have any luck yet,

    do anyone has some working code of clases example whit this information, or something to start to build a custom class?

    Thanks a lot for any help!

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 22, 2011 @ 18:58
    Tom Fulton
    0

    Hi Andres,

    I wrote a basic "Label" fieldtype the other day for a project, with an option to hide the caption, so it should work for both of your needs.  Here is the code below.  Will look at getting this added in the Contour Contrib package.  Let me know if you have any input or suggestions to improve it.  Thinking about letting it read the text from properties on a page eventually.

    public class Label : FieldType
        {
            [Umbraco.Forms.Core.Attributes.Setting("Text To Show",prevalues="",
                description="Enter the text to show in the label.",
                control="Umbraco.Forms.Core.FieldSetting.TextArea")]
            public string TextToShow {get;set;}

            [Umbraco.Forms.Core.Attributes.Setting("Hide Field Caption", prevalues = "",
                description = "Select to hide the label and only show the text",
                control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
            public string HideFieldCaption { get; set; }

            public List<Object> Value;

            public Label()
            {
                Id = new Guid("57ADD3AB-C3CD-464D-BCC2-3D1C05D3BF67");
                Name = "Label";
                Description = "A read-only field used to display blocks of text";
                Icon = "textfield.png";
                DataType = FieldDataType.String;
                Value = new List<object>();

            }

            public override bool HideLabel
            {
                get
                {
                    return Convert.ToBoolean(HideFieldCaption);
                }
                set
                {
                    base.HideLabel = value;
                }
            }

            public override System.Web.UI.WebControls.WebControl Editor
            {
                get
                {
                    System.Web.UI.WebControls.Panel pnl = new System.Web.UI.WebControls.Panel();
                    pnl.ID = "pnlHolder";
                    pnl.CssClass = "contourlabelfield";
                    pnl.Controls.Add(new System.Web.UI.LiteralControl("<p>" + umbraco.library.ReplaceLineBreaks(TextToShow) + "</p>"));
                    return pnl;
                }
                set
                {
                    base.Editor = value;
                }
            }

            public override bool SupportsRegex
            {
                get { return false; }
            }

            public override bool SupportsPrevalues
            {
                get { return false; }
            }

            public override string RenderPreview()
            {
                return "<p>" + umbraco.library.ReplaceLineBreaks(TextToShow) + "</p>";
            }

            public override string RenderPreviewWithPrevalues(List<object> prevalues)
            {
                return RenderPreview();
            }

            public override List<object> Values
            {
                get
                {
                    Value.Clear();
                    Value.Add(string.Empty);
                    return Value;

                }
                set
                {
                    Value = value;
                }
            }

        }

    Thanks,
    Tom

  • Andres Cano 22 posts 42 karma points
    Apr 22, 2011 @ 20:21
    Andres Cano
    0

    Hi Tom, wonderfull works perfect, i hope you can make it part of Contour Contrib Package,

    Thanks a lot for the help!

  • halo224 13 posts 33 karma points
    May 05, 2011 @ 10:08
    halo224
    0

    Hi Tom and all, thanks a lot for your code, it's really usefull for my last need.
    But I have a problem with the email received, without the 'TextToShow'. What did I do wrong?

    Label1 should be hidden (HideLabel = true) and the TextToShow should be visible (but no value is sended).

    Really thanks

    Name: 
    aaa
    Country: 
    bbb
    
    Label1:
    EMPTY...

    Comments: CCC
  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 05, 2011 @ 13:57
    Tom Fulton
    0

    Hi,

    The code above doesn't actually save the value of the label, which is why you wouldnt see anything in the email or entries viewer.

    You should be able to make it save the value by changing       Value.Add(string.Empty); to       Value.Add(TextToShow);

    Perhaps I will add an option to save or not save the value when I add it to the Contrib project - I've added a few other things also.

    Thanks,
    Tom

  • halo224 13 posts 33 karma points
    May 06, 2011 @ 09:52
    halo224
    0

    Hi Tom.
    Now the field is sent (and saved). My need was to clarify the email that the customer receives. Not necessarily to save, but just send.
    Sometimes 'TextToShow' may contains information about the fields below it. Example split shipping address with the billing or similar. 
    It would be interesting to be able to hide the caption (required by default) from email if we decided to hide it on the page (but this is a minor problem).
    Thanks a lot.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 06, 2011 @ 13:43
    Tom Fulton
    0

    Hi,

    I see what you mean.  I don't think there's any built in way to hide the caption from the email.  However you could do this by sending your own XSLT Transformed email and skipping that field.  I could possibly add an option for this and maybe adjust the Extended Workflow Emails in Contour Contrib to adhere to it, but it might be misleading because it won't affect the default email workflows..

    -Tom

  • Rajeev 126 posts 154 karma points
    Sep 27, 2011 @ 15:14
    Rajeev
    0

    Hi Tom,

    While playing around with the custom field type, I got into an error and the backend UI stopped loading for one form.

    Object reference not set to an instance of an object,  this was working fine till yesterday.

    The issue is in "umbraco.library.ReplaceLineBreaks(TextToShow)". Any inputs? It seems that TextToShow is null at the time of loading the backend UI.

    Will this cause any problems for a Field Group (No name provided) with the label as the only Field Type added in that. This was the only change I have made in the working form. The save was failing in between. I will be restoring the db back and trying this. If you have any inputs please let me know.

    Thanks,

    Rajeev

  • Rajeev 126 posts 154 karma points
    Sep 28, 2011 @ 08:37
    Rajeev
    0

    Hello Tom,

    Any inputs appreciated.  I  have restored the db back and everything started working again. But I am not able to Save the form after Moving the Label position to top and changing the text. I am still getting the same error. The database connection is bit slow. Will that cause this problem?

    {"Message":"Object reference not set to an instance of an object.","StackTrace":"   at umbraco.library.ReplaceLineBreaks(String text)\r\n   at Contour.SharedSource.Providers.FieldTypes.Label.RenderPreview()\r\n   at UmbracoContour.Webservices.Designer.RenderFieldPreview(String fieldTypeGuid, String[] prevalues)","ExceptionType":"System.NullReferenceException"}

    Not sure about the internal working of contour . It seems that that the TextToShow in the Code getting null values, I am not sure about why this is happening. Please let me know. Appreciate your help in this regard.

    thanks,

    Rajeev 

     

  • Rajeev 126 posts 154 karma points
    Sep 30, 2011 @ 15:03
    Rajeev
    0

    Hi,

    I think the issue was related to the db connectivity and not the field type. I think, it made the prev value error for not getting values. Once the db is restored and database connectivity up to norml, this issue didn't arise.

    Thank you,

    Rajeev

     

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Feb 16, 2012 @ 22:14
    Simon Dingley
    0

    Wondering if anyone can shed any light on why the public properties are rendering fine in the Contour UI but they are returning null when trying to access their values?

    [global::Umbraco.Forms.Core.Attributes.Setting("Text To Show", prevalues = "",
        description = "Enter the text to show in the label.", control = "Umbraco.Forms.Core.FieldSetting.TextArea")]
    public string TextToShow { get; set; }
    
    [global::Umbraco.Forms.Core.Attributes.Setting("Hide Field Caption", prevalues = "",
        description = "Select to hide the label and only show the text",
        control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
    public string HideFieldCaption { get; set; }
    
  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 16, 2012 @ 22:20
    Tom Fulton
    0

    Hi Simon,

    How/where are you trying to access these?  The class itself accesses them fine to determine whether to show/hide the label etc.  I might be misunderstanding though :)

    Also make sure you are using the latest source - I probably made some updates along the way after this was posted

    -Tom

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Feb 16, 2012 @ 22:44
    Simon Dingley
    0

    Hi Tom, I've grabbed the latest source but still when saving the field displays "failed to get preview" and stepping into the code the following line throws a null reference exception because "TextToShow" is null:

            public override string RenderPreview()
            {
                return "<p>" + umbraco.library.ReplaceLineBreaks(TextToShow) + "</p>";
            }

    While debugging the following was also throwing an exception because the value returned was "checked" and therefore not valid for parsing as a boolen:

            public override bool HideLabel
            {
                get
                {
                    return Convert.ToBoolean(HideFieldCaption);
                }
                set
                {
                    base.HideLabel = value;
                }
            }

    Perhaps things have changed in the latest version of Contour? The text I enter into the TextToShow field is saved in the form but I get no output on the clientside.

    Cheers, Simon

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 16, 2012 @ 23:00
    Tom Fulton
    1

    Hi Simon,

    You're right, something does appear to have changed with the checkbox.  As a quick workaround you could change the HideLabel getter to return HideFieldCaption == "checked";  and the check in the editor to  if (this.HideLabel).  This seems to work although the checkbox doesn't maintain it's state in the field properties - when you edit the field again it's unchecked.  I'll need to look more into this soon.

    The "failed to get preview" is normal - it should show the real text once you click save on the form.  I think it's a Contour limitation.

    With the above change it seems to work for me...again I'll look into a permanent fix when I get a few minutes, or let me know if you find anything :)

    -Tom

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Feb 17, 2012 @ 07:54
    Simon Dingley
    0

    Thanks Tom, managed to get it working ok now.

  • Rick 1 post 21 karma points
    Apr 26, 2013 @ 11:53
    Rick
    0

    heres my C# version - Umbraco 4.7.2 Contour 3.0.9.  Prob a few people out there like me still looking for a solution.

    Firstly, I'm a VB programmer and first time using Contour so feel free to make this code better, but after a lot of reading this is a combination of Tom's work above and others. It seems to work ok and covers most of the requests.  Label Titles and Label Text can optionally be hidden and different classes allow for the text or title to be styled independantly, and/or vary if one or the other are hidden.

    So from the beginning (incase you want to duplicate):

    1. Start a new C# class project using VS2010 (or VC#2010) or later as it must support .NET 4, otherwise the reference to Umbraco.Forms.Core, and any other referenced dll's compiled using .NET4, wont work.  Microsoft offer the Express versions of Visual Studio 2010 and 2012 for free at this time. VC# 2010 Express with VS2010 Sp1 installed is what I used to create this project.  VS2012 uses .NET4.5 so I stuck with VC#2010.
    2. The project must include references to: "System", "System.Web", "umbraco" and "Umbraco.Forms.Core".  The Umbraco/Contour dll's are usually in the website /bin folder.
    3. Then place all the code below in the clsContourLabel.cs file
    4. Finally compile the project and copy the .dll to your website /bin folder or copy the .cs file to the app_code directory. The website will now restart and your type will be registered automatically, no configuration needed.  I used the dll.

    My .cs file:

    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using Umbraco.Forms.Core;

    namespace nsContourLabel
    {
        public class ContourLabel : FieldType
        {
            [Umbraco.Forms.Core.Attributes.Setting("Text To Display", prevalues = "", description = "Enter the text to display.", control = "Umbraco.Forms.Core.FieldSetting.TextArea")]
            public string TextToShow { get; set; }

            [Umbraco.Forms.Core.Attributes.Setting("Hide Title", prevalues = "", description = "Select to hide the title", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
            public string HideFieldCaption { get; set; }

            [Umbraco.Forms.Core.Attributes.Setting("Hide Text", prevalues = "", description = "Select to hide the text", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
            public string HideFieldValue { get; set; }

            [Umbraco.Forms.Core.Attributes.Setting("Is Heading", prevalues = "", description = "Select to display the label and/or text as a heading", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
            public string IsHeading { get; set; }

            public System.Web.UI.WebControls.Label _lb;
            public List<Object> _value;
           
            public ContourLabel()
            {
                //Provider
                this.Id = new Guid("3F92E01B-29E2-4a30-BF33-9DF5580ED52D");
                this.Name = "Label";
                this.Description = "Renders a paragraph of text";
                this.Icon = "textfield.png";
                this.DataType = FieldDataType.LongString;
                _value = new List<object>();
            }

            public override WebControl Editor
            {
                get
                {
                    _lb = new System.Web.UI.WebControls.Label();
                    _lb.CssClass = "label-text";

                    if (IsHeading == "checked") _lb.CssClass +=" label-heading";            
                    if (HideFieldCaption == "checked" && HideFieldValue != "checked") _lb.CssClass += " label-nocaption";
                    if (HideFieldCaption != "checked" && HideFieldValue == "checked") _lb.CssClass += " label-novalue";
                    if (HideFieldCaption == "checked" && HideFieldValue == "checked") _lb.CssClass = "label-hidden";
                                   
                    _lb.Text = TextToShow;
                    return _lb;
                }
                set
                {
                    base.Editor = value;
                }
            }

            public override bool HideField
            {
                get
                {
                    return HideFieldValue == "checked";
                }
                set
                {
                    base.HideField = value;
                }
            }

            public override bool HideLabel
            {
                get
                {
                    return HideFieldCaption == "checked";
                }
                set
                {
                    base.HideLabel = value;
                }
            }

            public override bool SupportsRegex
            {
                get { return false; }
            }

            public override bool SupportsPrevalues
            {
                get { return false; }
            }

            public override string RenderPreview()
            {
                if (IsHeading == "checked")
                {
                    return  umbraco.library.ReplaceLineBreaks(TextToShow);
                }
                return "<p>" + this.TextToShow + "</p>";
            }

            public override string RenderPreviewWithPrevalues(List<object> prevalues)
            {
                return RenderPreview();
            }
        }
    }


Please Sign in or register to post replies

Write your reply to:

Draft