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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

If I place a form to a layout and call that layout for another side, form will not be displayed.


Go to End


9 Posts   1113 Views

Avatar
Liutauras

Community Member, 9 Posts

12 February 2015 at 6:51am

Edited: 12/02/2015 9:33am

Hi,

So I have noticed the following, if I place a form in a layout and will call that page directly then the form will be rendered. If I call it from another page then the form will not be displayed.

To be specific:

<?php
class ContactPage extends Page {
}
class ContactPage_Controller extends Page_Controller {
    private static $allowed_actions = array('Form');
    public function Form() { 
        $fields = new FieldList( 
            new TextField('Name'), 
            new EmailField('Email'), 
            new TextareaField('Message')
        ); 
        $actions = new FieldList( 
            new FormAction('submit', 'Submit') 
        ); 
        return new Form($this, 'Form', $fields, $actions); 
    }
}

Create ContactPage.ss with $Form entry only. From a different page call $renderWith(ContactPage) will result in no table output.

Avatar
thomas.paulson

Community Member, 107 Posts

12 February 2015 at 4:56pm

you can call 'ContactPage_Controller' Form to other page like below.

public function Form() {
$contact_page = ContactPage::get()->First();
$controller = new ContactPage_Controller($contact_page);
return $controller->Form();
}

also check the http://www.silverstripe.org/community/forums/form-questions/show/7334

Avatar
Liutauras

Community Member, 9 Posts

26 February 2015 at 10:21pm

Actually I cant. I use the following structure Page->Content->CustomPages and any php code that I add to either Content.php or Page.php and call it from the template Content.s does not get executed at all ! Yes I do add it to controler section of the page, yes the Content class extends Page class.

Avatar
thomas.paulson

Community Member, 107 Posts

26 February 2015 at 10:53pm

Hello Liutauras,

I does work well, I am attaching a screenshot.

Do you mind sharing php code, template file via http://www.sspaste.com/

Avatar
Liutauras

Community Member, 9 Posts

26 February 2015 at 11:27pm

Edited: 26/02/2015 11:39pm

Updated

Sure thing:

Page.php http://www.sspaste.com/paste/show/54eef35e9b275
Content.php http://www.sspaste.com/paste/show/54eef5bf53188
Content.ss http://www.sspaste.com/paste/show/54eef5903dbd5

More or less everything from the bootstrap template.

I have disabled rendering of other two tabs.

The only template file able to access any children of Page class is Page.ss. The only option I have is to cut out parts that require access to the base template and execute them in Page.ss file. If you have any more elegant ways of working around this let me know, that would be great.

Attached Files
Avatar
thomas.paulson

Community Member, 107 Posts

27 February 2015 at 12:08am

Place the $Form outside loop.
<div id="content">
$Form
</div>

01 <div id="content">
02 <% loop Menu(1) %>
03 $Form
04 $testContent
05 $testPage
06 $testUser
07 <section id="$URLSegment">
08 $renderWith($ClassName)
09 </section>
10 <% end_loop %>
11 </div>

Avatar
Nightjar

Community Member, 28 Posts

27 February 2015 at 1:25am

Initially reading the first post, it looked as though the ContactPage template was being used to render a different page directly. Which would make sense that no form would render, as it's simply a template, and another page does not have the contact for on it.
ie. the $Form variable template would refer to the current page's Form method, of which there is presumably none.

But now having seen the template, it makes more sense.

It looks like you're trying a single-page style layout for your website.
With that in mind, when you loop the Menu to get each page's data, you're getting the page model, where as a form is part of the controller.
So putting the Form outside of the loop is not the answer, as it will just print the form from the active page (the one loaded in the URL) - which is probably none (unless it's the contact page).

To get around this, you'd have to create a Form() method on the model, that returns

ModelAsController::controller_for($this)->Form();

Avatar
Liutauras

Community Member, 9 Posts

27 February 2015 at 1:27am

Thanks for suggestion, will give a shot!

Go to Top