FieldRules
FieldRules
FieldRules describes one field visibility rule of a content type. A rule holds conditions on field values and the actions that show or hide other fields when the conditions match.
When to Use This Class: Use this class to show a field only when another field has a given value. For example, a Video URL field appears only for video posts.
Conceptual Role: Acts as the data model of one item in ContentModelling.FieldRules, which the SDK serializes under the field_rules key.
Returns: FieldRules. One field visibility rule.
Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| MatchType | string? | No | null | Whether all or any conditions must match. Serialized as match_type. |
| Actions | List<Action>? | No | null | What happens to target fields when the rule matches. See Action. Serialized as actions. |
| conditions | List<Condition>? | No | null | The conditions the rule checks. See Condition. Serialized as conditions. |
Class-Level Notes
Setting rules.
- Add rules to ContentModelling.FieldRules and send the model with the Update method. The Set Field Visibility Rule for Content Type request of the Content Management API reference describes the rule object.
- The property name conditions is lowercase in the SDK, unlike MatchType and Actions.
- The SDK names this class Action, the same name as System.Action. A file that has both using System; and using Contentstack.Management.Core.Models.Fields; gets an ambiguous reference error (CS0104) on Action. Add an alias such as using FieldAction = Contentstack.Management.Core.Models.Fields.Action;, or write the full name.
Method Index
FieldRules has no methods. Set its properties and add the object to ContentModelling.FieldRules.
Class-Level Snippet
The snippet below updates a content type with a rule that shows the video URL field only when the post type is video.
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;
using FieldAction = Contentstack.Management.Core.Models.Fields.Action;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ContentModelling model = new ContentModelling
{
Title = "Blog Post",
Uid = "blog_post",
Schema = new List<Field>
{
new TextboxField { DisplayName = "Title", Uid = "title", DataType = "text", Mandatory = true, Unique = true },
new TextboxField { DisplayName = "Post Type", Uid = "post_type", DataType = "text" },
new TextboxField { DisplayName = "Video URL", Uid = "video_url", DataType = "text" }
},
FieldRules = new List<FieldRules>
{
new FieldRules
{
MatchType = "all",
conditions = new List<Condition>
{
new Condition { OperandField = "post_type", Operator = "equals", Value = "video" }
},
Actions = new List<FieldAction>
{
new FieldAction { state = "show", TargetField = "video_url" }
}
}
}
};
try
{
ContentstackResponse response = client.Stack("<API_KEY>").ContentType("blog_post").Update(model);
Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}