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.

All other Modules /

Discuss all other Modules here.

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

UserDefinedForms - render form within another pages template


Go to End


5 Posts   1207 Views

Avatar
Vix

Community Member, 60 Posts

16 December 2015 at 9:22pm

Edited: 16/12/2015 9:23pm

Is it possible to render a UserDefinedForm within the template of another page?

I have a page set up with multiple DataObjects (being a block of content) and I need to render the form within these.

I have

class Page extends SiteTree {
	
	private static $has_many = array(
		'ContentPanels' => 'ContentPanel'
	);
}

Content Panel

<?php class ContentPanel extends DataObject {
	private static $has_many = array(
		'Forms' => 'FormHolder'
	);
?>

FormHolder

<?php class FormHolder extends DataObject {
	
	private static $db = array(
		'Title' => 'Text',
		'Content' => 'HTMLText',
		'SortOrder' => 'Int'
	);

	private static $has_one = array(
		'UserForm' => 'UserDefinedForm',
		'ContentPanel' => 'ContentPanel'
	);
?>

Is this possible to get the form to display and how might I do it?

Avatar
Lexandclo

Community Member, 55 Posts

17 December 2015 at 4:17am

Try adding this in the Page types you want the form rendering.

public function showForm() { 
      $record = DataObject::get_one("UserDefinedForm", "URLSegment = 'url-of-form'"); 
      $results = new UserDefinedForm_Controller($record); 
      return $results;
	} 

then use this in the ss template

<% with $showForm %>
						    
						
						        $Form
						    
						    <% end_with %>

Hope that helps

Avatar
Vix

Community Member, 60 Posts

17 December 2015 at 5:10pm

Awesome! Thankyou!

Although because the form is being displayed within a loop I had to change the template code to:

<% with $Top.showForm %>
                  $Form
                <% end_with %>

Thanks again!

Avatar
Lexandclo

Community Member, 55 Posts

17 December 2015 at 10:09pm

Glad it worked

Avatar
Vix

Community Member, 60 Posts

18 December 2015 at 5:35pm

Edited: 18/12/2015 5:37pm

Just curious if you have any ideas on this...

As the template file was getting rather long I separated each content panel into its own include file. Which works fine, for everything except the one with the form in it. The code works if it is just on Page.ss but if it is in an include it does not return anything.

I tried changing the $Top.showForm to $Parent.showForm or $Up.showForm but that did not do it either.

Not really a big deal, but just curious.