Nibble

Archive for December, 2012

Contour code first and Conditional Logic 5

Another code first example, setting up field conditions when designing your form in visual studio.

There are 3 properties on the Field attribute that need to be used (EnableCondition, ConditionActionType and ConditionLogicType) then for setting up 1 or multiple rules you’ll need to decorate the property with a FieldConditionRule attribute providing it the caption of the field, the rule operator and the value.

        [Field("Leave a comment", "Your comment",
            Mandatory = true)]
        public string Name { get; set; }
 
        [Field("Leave a comment", "Your comment",
            EnableCondition = true,
            ConditionActionType = FieldConditionActionType.Show,
            ConditionLogicType = FieldConditionLogicType.Any)]
        [FieldConditionRule("Name",FieldConditionRuleOperator.Is, "Test")]
        public string Hidden { get; set; }

In this case the field with caption hidden will only be shown if the value of the field with caption Name is test

Contour code first and custom fieldtypes 4

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.