Developing custom form layouts

The layout of forms composed via the Kentico form builder is based on elements called sections. Each form section defines a block of HTML code containing one or more zones. These zones can then hold any number of form fields (based on form components).

When creating forms in the form builder interface of the Forms application, editors first add or adjust the sections, and then add individual fields into the resulting zones.

The system provides a Default form section which organizes fields in a basic single-column layout (one zone). The default section is automatically added into new forms, and in cases where an editor removes the last section from a form. If you wish to use more advanced layouts for fields in your forms, you need to develop and register your own form sections.

Form submit button

The submit button of a form is rendered after the last section (when displaying forms on the website using the Form widget). If you need to adjust the position of the submit button, use appropriate CSS styling in your site's stylesheet.

On this page

Developing form sections

Form sections are implemented as controllers that return HTML markup containing form zones.

Implementing form section controllers

When creating controllers for form sections, implement the default Index action which retrieves the section markup. The action must return the section's HTML content, typically a partial view.

The recommended location to store form section controllers is the ~/Controllers/FormSections folder.

Do not disable POST requests for the Index action

POST requests cannot be disabled for the Index action (e.g., by using the HttpGet attribute). POST requests to the Index action are used by the form builder feature.

    public class TwoColumnFormSectionController : Controller
    {
        // Action used to retrieve the section markup
        public ActionResult Index()
        {
            return PartialView("FormSections/_TwoColumnFormSection");
        }
    }

Registering form sections

Register sections by adding the RegisterFormSection assembly attribute to the controller class. Specify the following attribute parameters:

  • Identifier – string identifier of the form section. We recommend using sufficiently unique identifiers to avoid potential conflicts with other third-party sections, for example using a company name prefix.
  • Controller type – the System.Type of the form section controller class.
  • Name – the name of the form section. Displayed when selecting sections on the Form builder tab in the administration interface.
  • (Optional) Description – a description of the form section. Displayed as a tooltip when selecting sections on the Form builder tab in the administration interface.
  • (Optional) IconClass – the font-icon assigned to the form section. Displayed when selecting sections on the Form builder tab in the administration interface. For a list of font icons available by default in the system, see Kentico icon list.

using Kentico.Forms.Web.Mvc;
 
[assembly: RegisterFormSection("LearningKit.FormSections.TwoColumns", typeof(TwoColumnFormSectionController), "Two columns", Description = "Organizes fields into two equal-width columns side-by-side.", IconClass = "icon-l-cols-2")]

Localizing section metadata

Both the Name and the Description of form sections can be localized using resource string keys. See Localizing MVC builder components.

The section is now available in the form builder interface.

Implementing form section views

Create partial views with the required formatting. Use the FormZone extension method to identify locations where form fields can be placed.

The recommended location for the view files is the ~/Views/Shared/FormSections folder.

Example
@using Kentico.Forms.Web.Mvc
@using Kentico.Web.Mvc

<div>
    <div style="float:left; width:50%;">
        @Html.Kentico().FormZone()
    </div>
    <div style="float:left; width:50%;">
        @Html.Kentico().FormZone()
    </div>
    <div style="clear:both;" />
</div>

Adding CSS styles for form sections

Use the following approach to add CSS styles for your form sections:

  • For basic styles that are required for the section to render correctly, create stylesheet files in sub-folders under the ~/Content/FormSections directory of your MVC project (you may need to create the FormSections directory). Use sub-folders that match the identifiers of individual sections.
  • If you wish to provide additional styling for the Kentico administration interface (for example to ensure that the sections are in line with the overall look and feel of the admin UI), add another stylesheet file to the same directory with the .admin.css extension.
  • Any site-specific styles that finalize the live site design of the form section should be handled separately within the given site's main stylesheet.

    To avoid potential conflicts between styles from other third-party form components or sections, we recommend adding a unique prefix to your CSS classes and identifiers (for example #CompanyName-mid-column), or employ similar measures to ensure their uniqueness.

The system automatically creates bundles containing all .css files located under ~/Content/FormSections – one for general styles and another for the administration interface styles. The bundles are then linked in the following locations:

  • When working with forms in the Forms application of the Kentico administration interface.
  • The bundle containing general styles is linked on all pages with page builder editable areas (the page builder is used to display forms on the live site via the Form widget).

The same bundles also contain styles added for form components in the ~/Content/FormComponents directory.

CSS class order

Do not make any assumptions about the relative order of the source CSS in the resulting bundles – individual files contained in the bundle may or may not precede each other.

Adding scripts for form sections

If your form sections require any JavaScript, place script files into sub-folders under the ~/Content/FormSections directory of your MVC project (you may need to create the FormSections directory). Use sub-folders that match the identifiers of individual sections or a Shared sub-folder for assets used by multiple sections.

The system automatically creates a bundle containing all .js files located under ~/Content/FormSections. The bundle is then linked in the following locations:

  • When working with forms in the Forms application of the Kentico administration interface.
  • On all pages with page builder editable areas (the page builder is used to display forms on the live site via the Form widget).

The same bundle also contains script files added for form components in the ~/Content/FormComponents directory.

Initializing section scripts

If you need to initialize scripts from the views of form sections (for example register an event listener), we recommend adding HTML Event Attributes to the section's elements.

The system does NOT run scripts defined in section views when displaying the form builder and page builder editing interface. If you only need to run a script on page load for the live site, you can use a DOMContentLoaded event listener.

Apart from initialization code, avoid linking or executing scripts directly within form section views. This could lead to duplicated scripts for forms that contain multiple instances of the same section, or on pages with multiple forms.

Using jQuery scripts

By default, Kentico links two system jQuery bundles into the pages of the form builder interface. The system jQuery version is 3.3.1. If you wish to use a different version of jQuery within your form sections, you need to create your own bundle(s) with the corresponding paths:

  • ~/bundles/jquery
  • ~/bundles/jquery-unobtrusive-ajax – bundle for using the jquery.unobtrusive-ajax JavaScript library

When you register a bundle with one of these paths, the form builder interface links your jQuery bundle instead of the corresponding system bundle.

Important: When you register a custom jQuery bundle, the system no longer links the default jQuery bundle on pages with page builder editable areas (the page builder is used to display forms on the live site via the Form widget). You need to manually link your custom jQuery bundle on the given pages (either within the used layout or directly in the page's view).

For more information, see Creating pages with editable areas in MVC.


Was this page helpful?