Copied to clipboard

Flag this post as spam?

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


  • Mike Fayer 1 post 71 karma points
    Nov 01, 2017 @ 13:27
    Mike Fayer
    0

    Is there a way to paramaterise prevalues, or some other way to get prevalues from the form parent?

    Hi all - I'm a bit of a newbie so bear with me. I have what seems like a simple requirement: In an Umbraco-form there should be a dropdown whose choices should consist of all the children of the parent of that form, of a certain document-type.

    Prevalues almost work here, because I can set them to the correct document type. But, the prevalues somehow need to also be constrained so that they are only descendants of whichever piece of content is hosting the form.

    Is there any way to accomplish this, or am I thinking of the problem wrong? Thanks!

  • Alex Brown 129 posts 620 karma points
    Nov 06, 2017 @ 08:57
    Alex Brown
    0

    Have you tried creating a custom field type?

    https://our.umbraco.org/documentation/Add-ons/umbracoforms/developer/extending/Adding-a-Fieldtype

    All you need is a class and a view to render your custom Razor/HTML.

    Let me know if you need help.

    Edit - in fact I have an example with select/optgroup/option elements:

    C# class:

    public sealed class ProductList : HiddenField
    {
        public ProductList()
        {
            Id = new Guid("b0643a6b-01a8-45bb-baf0-333daf8396d7");
            FieldTypeViewName = "FieldType.ProductList.cshtml";
            Name = "ProductList";
            Description = "Renders a dropdown containing a list of products.";
            SortOrder = 1;
            Icon = "icon-list";
            DataType = FieldDataType.String;
        }
    }
    

    Razor/HTML:

    @model Umbraco.Forms.Mvc.Models.FieldViewModel
    
    @{
        var helper = new UmbracoHelper(UmbracoContext.Current);
        var currentPage = helper.TypedContent(UmbracoContext.Current.PageId);
        var products = currentPage.DocumentTypeAlias.Equals(Literals.DocumentTypes.ProductRegistration)
            ? currentPage["productRegistrationList"] as dynamic
            : null;
    }
    
    @if (products != null)
    {
        <label class="fieldLabel">
            Please select a product
        </label>
        <select name="@Model.Name" id="@Model.Id">
            @renderOptionsForDropdownList(products)
        </select>
    }
    
    @helper renderOptionsForDropdownList(dynamic list)
    {
        foreach (var item in list)
        {
            <optgroup label="@item["productRegistration"]">
                @foreach (var product in item["productList"])
                {
                    if (!string.IsNullOrEmpty(product))
                    {
                        <option value="@product">
                            @product
                        </option>
                    }
                }
            </optgroup>
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft