SS4: add HTML to a FormField label

Everyone knows that "Terms and Conditions" checkbox in forms. "By signing up you agree to..." with a link to the terms and condiditions. Fortunately SilverStripe escapes all HTML we pass as a Title of a FormField. But it wasn't SilverStripe when ther wasn't an easy trick for passing that link to the label.

In this case you have to pass a kind of "HTMLFragment" as Title like:

CheckboxField::create(
    'TermsAndConditions', 
    DBField::create_field(
        'HTMLFragment', 
        'By clicking you accept our <a href="#" title="Do not click!">Terms and Conditions</a>'
    )
);

Internally HTMLFragment maps to HTMLText, but the difference is, that HTMLFragment disables shortcode parsing.

SilverStripe's casting documentation says:

  • HTMLFragment is a block of raw HTML, which should not be escaped. Take care to sanitise any HTML value saved into the database.
  • HTMLText is a HTMLFragment, but has shortcodes enabled. This should only be used for content that is modified via a TinyMCE editor, which will insert shortcodes.

 Thanks to kinglozzer for pointing out this solution on a question by brew_jay on slack, #ss4.

Theoretically this solution should also work on SilverStripe 3.

 

Rate this post (1 ratings)

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments