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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

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


Go to End


4 Posts   2003 Views

Avatar
vwd

Community Member, 166 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, 166 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, 5523 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, 166 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.