3

We're currently using Umbraco version 7.1.4 assembly: 1.0.5261.28127 with Contour version 3.0.26

I'm trying to populate a contour form with information pulled from a database, but dependent on a user cookie (the cookie hold the primary key for the record in the database).

To implement this I'm looking at writing a custom field type (well a bunch of them, one for each data field) which examines the cookie makes the db request and then populates the textbox with the value (users name/address/etc).

I've managed to add custom setting to a control and have it display the value that's populated at design time, but I can't seem to amend that value at run time.

I'm happy to post the code if relevant, but my question is. Am I barking up the wrong tree? is this the best way to handle this or would it even work?

Any pointers would be most welcome

Thanks

EDIT

Thanks Tim, I've now managed to break it in such a way it's not even rendering the controls (the debug message is saying the SVT value doesn't exist). This just (or should) just populate the form with the current date/time just to get something working.

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

namespace Custom.FieldType
{
    public class CustomTextfield : Umbraco.Forms.Core.FieldType
    {
        public CustomTextfield()
        {
            //Provider 
            this.Id = new Guid("b994bc8b-2c65-461d-bfba-43c4b3bd2915");
            this.Name = "Custom Textfield";
            this.Description = "Renders a html input fieldKey"; //FieldType 
            this.Icon = "textfield.png";
            this.SVT = DateTime.Now.ToLongTimeString();
        }

        public System.Web.UI.WebControls.TextBox tb;
        public List<Object> _value;

        [Umbraco.Forms.Core.Attributes.Setting("SVT", description = "the SVT")]
        public string SVT { get; set; }

        public override WebControl Editor
        {
            get
            {
                tb.TextMode = System.Web.UI.WebControls.TextBoxMode.SingleLine;
                tb.CssClass = "text gaudete";


                if (_value.Count > 0)
                    tb.Text = _value[0].ToString();

                SVT = DateTime.Now.ToLongTimeString();
                tb.Text = tb.Text + SVT;

                return tb;
            }
            set { base.Editor = value; }
        }

        public override List<Object> Values
        {
            get
            {
                if (tb.Text != "")
                {
                    _value.Clear();
                    _value.Add(tb.Text);
                }
                return _value;
            }
            set { _value = value; }
        }

        public override string RenderPreview()
        {
            return
                "<input type=\"text\" id=\"text-content\" class=\"text\" maxlength=\"500\" value=\"" + this.SVT + "\" />";
        }

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

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

    }
}

And the view is

@model Umbraco.Forms.Mvc.Models.FieldViewModel
@{
    var widthSetting = Model.AdditionalSettings.FirstOrDefault(s => s.Key.Equals("Width"));
    string width = (widthSetting == null) ? null : widthSetting.Value;
    var textSetting = Model.AdditionalSettings.FirstOrDefault(s => s.Key.Equals("SVT"));
    string widthTXT = (textSetting == null) ? null : textSetting.Value;
}
<input type="text" name="@Model.Name" id="@Model.Id" class="text"  maxlength="500"  

        value="@{if(!string.IsNullOrEmpty(widthTXT)){<text>@(SVT)</text>}}"

    @{if(Model.Mandatory || Model.Validate){<text>data-val="true"</text>}}
    @{if (Model.Mandatory) {<text> data-val-required="@Model.RequiredErrorMessage"</text>}}
    @{if (Model.Validate) {<text> data-val-regex="@Model.InvalidErrorMessage" data-regex="@Model.Regex"</text>}}
/>

The code is mostly cobbled together from online tutorials which is why the naming is abysmal but if I can get something to populate the text box on the clients side then I can start the process of refactoring (well scrapping this demo version and writing a real version)

Thanks.

EDIT2

I was able to fix the error stopping the view loading thanks to the pointer from Tim, the new view looks as follows

@model Umbraco.Forms.Mvc.Models.FieldViewModel
@{
    var textSetting = Model.AdditionalSettings.FirstOrDefault(s => s.Key.Equals("SVT"));
    string widthTXT = (textSetting == null) ? null : textSetting.Value;
}
<input type="text" name="@Model.Name" id="@Model.Id" class="text"  maxlength="500"  

        value="@{if(!string.IsNullOrEmpty(widthTXT)){<text>@(widthTXT)</text>}else{<text>Unknown</text>}}"

    @{if(Model.Mandatory || Model.Validate){<text>data-val="true"</text>}}
    @{if (Model.Mandatory) {<text> data-val-required="@Model.RequiredErrorMessage"</text>}}
    @{if (Model.Validate) {<text> data-val-regex="@Model.InvalidErrorMessage" data-regex="@Model.Regex"</text>}}
/>

And just displays "Unknown" in the text box

thanks again.

  • 1
    Can you post your code? What you're trying to do should in theory be possible. – Tim Oct 03 '16 at 10:33
  • Thanks @Tim , It's helpful at least to know it should be possible :) –  Oct 04 '16 at 07:12
  • 1
    Try changing @(SVT) to @Model.SVT, does that work? It's currently erroring because you don't have a variable called SVT in the View. – Tim Oct 04 '16 at 09:40
  • Thanks @Tim (at)Model.SVT didn't help, but changing the it to widthTXT at least got it to load (this is the state I had it in the night before I posted the question :) ) –  Oct 04 '16 at 16:25

0 Answers0