Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

[Solved] Overriding Form.ss without breaking CMS admin interface


Reply


4 Posts   486 Views

Avatar
vwd

Community Member, 160 Posts

18 February 2014 at 1:01pm

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.

Avatar
vwd

Community Member, 160 Posts

18 February 2014 at 3:08pm

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...

Avatar
Willr

Forum Moderator, 5513 Posts

22 February 2014 at 9:32pm

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

Avatar
vwd

Community Member, 160 Posts

22 February 2014 at 9:48pm

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.