Copied to clipboard

Flag this post as spam?

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


  • Tom Smith 98 posts 172 karma points
    Apr 11, 2012 @ 13:29
    Tom Smith
    0

    Default Value for custom field type

    Hi All,

    I've created a custom field type for contour which is basically just a drop down list, (of countries available for trading / shipping as defined in Tea Commerce)

    It renders;

    <select>
    <option value="">Please Choose...</option>
    <option value="1">A Country</option>
    <option value="2">Another Country</option>
    </select>

    I want to be able to set a default value based on a custom member property; i.e. I would like to supply {member.country} to the DefaultValue field and have contour add the 'selected' attribute to the correct <option> e.g. if {member.country} = 2 then 'Another Country' should be selected.

    So how do I implement DefaultValue on a custom field type? Do I have to do it all myself (check if curly brackets in string, get hold of current member, look up correct property etc etc) or just overide something somewhere?

    Many Thanks,

    Tom

  • Comment author was deleted

    Apr 11, 2012 @ 13:38

    Well you need to add a default value setting and then add some code to set the value of the control to the default value, you don't need to worry about the member stuff, just using the shortcuts should give you the right value.

    So you'll need to add

     

     [Attributes.Setting("DefaultValue", description = "Enter a default value", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
     public string DefaultValue { get; set; }

    And this is a snippet from the default dropdownlist where it checks the default value and adds the list items

                        string dv = string.Empty;
                        if (DefaultValue != null && DefaultValue != string.Empty)
                            dv = StringHelper.ParsePlaceHolders(HttpContext.Current, DefaultValue);
     
     
                        foreach (var prevalue in this.AssociatedField.PreValueSource.Type.GetPreValues(this.AssociatedField))
                        {
                            ListItem li = new ListItem(Umbraco.Forms.Data.DictionaryHelper.GetText(prevalue.Value), prevalue.Id.ToString());
     
                            if (dv != string.Empty){
                                if (li.Text == dv)
                                    li.Selected = true;
                            }
     
                            dd.Items.Add(li);
                        }

     

  • Tom Smith 98 posts 172 karma points
    Apr 11, 2012 @ 13:56
    Tom Smith
    0

    Hi Tim,

    Brilliant, Thanks,

    StringHelper.ParsePlaceHolders(HttpContext.Current, DefaultValue);

    Was what I was after :)

    Many Thanks,

    Tom

Please Sign in or register to post replies

Write your reply to:

Draft