Include userform on page

Silverstripe Version: 4.2

How do I include a userform i have created on page.php?
In silverstripe 3 i used to use the below code in the pageContoller but it no longer works in SS4.
I know that UserDefinedForm_Controller is now UserDefinedFormController but i still get errors

function ShowForm(){ 
        $get = DataObject::get_one('SiteTree', "URLSegment = 'contact-us'"); 
        return new UserDefinedForm_Controller($get); 
    }

Please post the errors you’re getting

Not too clear on the end goal here, but your ShowForm() method appears to be returning a controller rather than just a form, is that what you want?

You could try ModelAsController::controller_for($siteTreeObject)->Form()

1 Like

Thanks JonoM. Your solution worked when I wanted to reuse a UserDefinedForm

I’m also trying to do something similar. In SS3 I was simply able to subclass the user forms page type to have the form appear within a page.

Worked nicely, but with upgrade to SS4.2.1, the form will work fine until I run the next dev/build, then page containing forms appears ‘modified’ in CMS and loses all its form fields. Inspecting the database via phpmyadmin, I can see that all the parent classes get reset for the EditableFormFields and UserDefinedForm_EmailRecipients.

Not quite sure whether I’m using it wrong or it’s a bug?

Hi Ketut_Gunaksa,

Add the following codes to the Page.php page:

use SilverStripe\UserForms\Control\UserDefinedFormController;
use SilverStripe\UserForms\Model\UserDefinedForm;

private static $allowed_actions = ;

Then write the id number of the form you created in the code below:

public function NewsletterForm(){
$page = UserDefinedForm::get()->byID(22);
$form = (new UserDefinedFormController($page))->Form();
return $form;
}

Finally, it is enough to put the following code on the .ss page.

<% loop NewsletterForm %><% loop FormFields %>$Form <% end_loop %><% end_loop %>