Nibble

Contour code first and custom fieldtypes

A question that popped up several times regarding the new Contour code first feature is if it’s possible to use custom fieldtypes.

It sure is, take a look at the following example. Let’s say we want to use a fieldtype from the Contour Contrib project.

First we’ll need to add a reference to the assembly containing the custom fieldtype we want to use

image

Once that is in place we can simply set the type of field to the type of the fieldtype you wish to use

[Field("Leave a comment", "Your comment",
           Mandatory = true,
           Type = typeof(Contour.Contrib.FieldTypes.ReCaptcha))]
        public string Captcha { get; set; }

And that’s basically it!

If the custom fieldtype has additional settings you can also provide a value for those with the following syntax

 [Field("Leave a comment", "Your comment", 
           "Language:en","Theme:clean",
           Mandatory = true,
           Type = typeof(Contour.Contrib.FieldTypes.ReCaptcha))]
        public string Captcha { get; set; }

 

Of course to deploy this we’ll need to deploy the necessary references (like contour contrib and recaptcha in this case) to your umbraco site.

4 Comments so far

  1. Martin Griffiths on December 7th, 2012

    How do I tie a Prevalue source type to Code First form? The attribute only accepts an simple array.

  2. game iwin on December 10th, 2012

    Thanks. it is great

  3. Tim Geyssens on December 10th, 2012
  4. eric schrepel on March 26th, 2013

    You seem like the smartest guy on the planet related to Contour stuff, so two quick questions though I can forward them to the our.umbraco forum as well:

    1) Can’t figure out how to add a ReCaptcha field to a form that is bound to a SQL datasource, because it seems each form element must tie to a SQL field, and not sure what field type might work. Keep getting “Object reference not set…” errors. I don’t need to store the ReCaptcha result, just need to use it as validation.

    2) Any way to add/delete form fields after having a created a form based on a SQL datasource? I see no or X icons in the form designer. So if I *do* get ReCaptcha working, seems I’d have to delete and recreate the form.

    Using Umbraco 6.0.3 and Contour 3.0.9, and not a die-hard programmer so doing most things through the UIs or some simple VisualStudio/Razor stuff.

    Thanks for any assistance.

Leave a Reply