Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » [Solved] Overriding Form.ss without breaking CMS admin interface

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 323 Views
  • vwd
    Avatar
    Community Member
    158 Posts

    [Solved] Overriding Form.ss without breaking CMS admin interface Link to this post

    Hi,

    I had a need to modify Form.ss slightly so that I could wrap the global error/notification message in a <span>. This is intended to be used for front-end forms only.

    However when I did this, it seemed to break the CMS admin interface. Rather than use the appropriate template eg. CMSMain_EditForm.ss it was picking up my (overridden) version of Form.ss located in mysite/templates.

    Is this expected behaviour? Why does an overridden Form.ss take precedence over more specific templates such as CMSMain_EditForm.ss? Is there another way of achieving the simple thing I'm trying to do of wrapping the global error message text in a <span>.

    This is the overridden Form.ss

    <% if $IncludeFormTag %>
    <form $AttributesHTML>
    <% end_if %>
       <% if $Message %>
       <p id="{$FormName}_error" class="message $MessageType"><span>$Message</span></p>
       <% else %>
       <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
       <% end_if %>
       
       <fieldset class="thisistheone">
          <% if $Legend %><legend>$Legend</legend><% end_if %>
          <% loop $Fields %>
             $FieldHolder
          <% end_loop %>
          <div class="clear"><!-- --></div>
       </fieldset>

       <% if $Actions %>
       <div class="Actions">
          <% loop $Actions %>
             $Field
          <% end_loop %>
       </div>
       <% end_if %>
    <% if $IncludeFormTag %>
    </form>
    <% end_if %>

    Thank you.
    VWD.

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: [Solved] Overriding Form.ss without breaking CMS admin interface Link to this post

    The solution was simple enough. Rather than overriding Form.ss, I subclassed the form object and created a new template for it.

    So I created the following:

    MyForm.php

    <?php

    class MyForm extends Form {
       // …
    }

    MyForm.ss

    <% if $IncludeFormTag %>
    <form $AttributesHTML>
    <% end_if %>
       <% if $Message %>
       <p id="{$FormName}_error" class="message $MessageType"><span>$Message</span></p>
       <% else %>
       <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
       <% end_if %>
       
       <fieldset class="thisistheone">
          <% if $Legend %><legend>$Legend</legend><% end_if %>
          <% loop $Fields %>
             $FieldHolder
          <% end_loop %>
          <div class="clear"><!-- --></div>
       </fieldset>

       <% if $Actions %>
       <div class="Actions">
          <% loop $Actions %>
             $Field
          <% end_loop %>
       </div>
       <% end_if %>
    <% if $IncludeFormTag %>
    </form>
    <% end_if %>

    Hope this helps...

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: [Solved] Overriding Form.ss without breaking CMS admin interface Link to this post

    FYI you don't need to subclass for this. You can use $form->setTemplate() to dynamically set the template name to use.

    http://api.silverstripe.org/3.1/class-Form.html#_setTemplate

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: [Solved] Overriding Form.ss without breaking CMS admin interface Link to this post

    OK - thanks for letting me know. I had to subclass Form for another reason anyway, but it's good to know that I didn't have to and and that the template can be explicitly set.

    323 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.