Copied to clipboard

Flag this post as spam?

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


  • Tim Mather 13 posts 105 karma points
    Nov 09, 2017 @ 15:33
    Tim Mather
    0

    Best way to populate fields on UmbracoForms

    Any suggestions on how we go about populating the form fields of a Forms based form when passing in a record id. Currently we are using a javascript string to run through all form field types and add to the form once the page is loaded but would ideally like to do it using MVC in someway through the model rather than through javascript? This is the function we run to gen the javascript at the moment.

    public static string GenerateJavascriptToPopuplateFormFromRecord(Guid recordGuid, Guid formGuid)
    {
        FormStorage fs = new FormStorage();
        Form form = fs.GetForm(formGuid);
    
        RecordStorage rs = new RecordStorage();
        Record record = rs.GetRecordByUniqueId(recordGuid, form);
    
        StringBuilder js = new StringBuilder();
    
        // populate answers
        if (record != null)
        {
            if (record.RecordFields != null && record.RecordFields.Count() > 0)
            {
                foreach (var field in record.RecordFields)
                {
                    if (field.Value != null && field.Value.Field != null)
                    {
                        var type = CGHelper.CleanString(field.Value.Field.FieldType.Name).ToLower();
                        var value = field.Value.ValuesAsString().NullSafeToString();
    
                        switch (type)
                        {
                            case "multiplechoice":
                            case "quizcheckboxlist":
    
                                if (field.Value.Values.Count() > 0)
                                {
                                    foreach (var v in field.Value.Values)
                                    {
                                        js.AppendLine("$('#" + field.Value.FieldId + "').find('input[value=" + v.ToString().Trim() + "]').prop('checked', 'checked');");
                                    }
                                }
    
                                break;
    
                            case "singlechoice":
                            case "quizradiobuttonlist":
    
                                if (value != String.Empty)
                                {
                                    js.AppendLine("$('#" + field.Value.FieldId + "').find('input[value=" + value + "]').prop('checked', 'checked');");
                                }
    
                                break;
    
                            case "checkbox":
    
                                if (value.ToLower() == "true")
                                {
                                    js.AppendLine("$('#" + field.Value.FieldId + "').prop('checked', 'checked');");
                                }
    
                                break;
    
                            case "date":
                                break;
    
                            case "dropdown":
    
                                if (value != String.Empty)
                                {
                                    js.AppendLine("$('#" + field.Value.FieldId + "').val('" + value + "');");
                                    js.AppendLine("$('#" + field.Value.FieldId + "').parents('.selector').first().find('span').text('" + value + "');");
                                }
    
                                break;
    
                            case "fileupload":
    
                                if (value != String.Empty)
                                {
                                    js.AppendLine("$('#" + field.Value.FieldId + "').val('" + value + "');");
                                    js.AppendLine("$('#" + field.Value.FieldId + "').parents('.form-group').append('<a href=\"" + value + "\" download=\"" + value + "\" target=\"_blank\">" + value + "</a>');");
                                }
    
                                break;
    
                            default:
    
                                if (value != String.Empty)
                                {
                                    js.AppendLine("$('#" + field.Value.FieldId + "').val('" + value + "');");
                                }
    
                                break;
                        }
                    }
                }
    
                var toReturn = new StringBuilder();
                toReturn.AppendLine("console.log(\"fired\");");
                toReturn.AppendLine("<script type=\"text/javascript\">");
                toReturn.AppendLine("$(document).ready(function() {");
                toReturn.AppendLine(js.ToString());
                toReturn.AppendLine("});");
                toReturn.AppendLine("</script>");
    
                return toReturn.ToString();
            }
            else
            {
                return string.Empty;
            }
    
        }
        else
        {
            return string.Empty;
        }        
    }
    

    Better suggestions welcome!

  • Frans de Jong 522 posts 1762 karma points c-trib
    Nov 17, 2017 @ 15:38
    Frans de Jong
    0

    If you use the default insertforms script you can add the recordId in the querystring. That should get the entry you want to edit.

    You can see it in the InsertUmbracoFom.chtml below:

     @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @if (Model.MacroParameters["FormGuid"] != null)
    {
        var s = Model.MacroParameters["FormGuid"].ToString();
        var g = new Guid(s);
    
        var recordGuid = Guid.Empty;
    
        if (string.IsNullOrEmpty(Request.QueryString["recordId"]) == false)
        {
            Guid.TryParse(Request.QueryString["recordId"], out recordGuid);
        }
    
        Html.RenderAction("Render", "UmbracoForms", new {formId = g, recordId = recordGuid });
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies