Silverstripe Multiple Custom Forms on One Page

I am trying to create a single page that displays multiple user forms as tabs. For example, a basic contact form, a quote form request, etc.

It seemed to me that I could create a new page type and scroll through the children to display the forms, but the $ Form variable does not display the form.

<% loop $Children %>
   <div>
     <h2>$Title</h2>
     $Form
   </div>
<% end_loop %>

Am I missing something, or is there another way to render the form using its identifier in the template file?

+4
source share
3 answers

You can try the following.

, ( UserDefinedForm). .

public function ChildForm($pageID) {
  $page = UserDefinedForm::get()->byID($pageID);
  $controller = UserDefinedForm_Controller::create($page);
  return $controller->Form();
}

<% loop $Children %>
   <div>
     <h2>$Title</h2>
     $Top.ChildForm($ID)
   </div>
<% end_loop %>

( ) , .

+4

DataObject/Page Controller. $Children DataObject, Form UserDefinedForm.

, :

  • , , .
  • , UserDefinedForm

, .

, Page ( ):

function getInLoopForm() {
    if (in_array('UserDefinedForm', $this->ClassAncestry)) {
        $controllerName = $this->ClassName . '_Controller';
        $controller = $controllerName::create($this);
        if ($controller->hasMethod('Form')) {
            return $controller->Form();
        }
    }
    return false;
}

, UserDefinedForm . , .

:

<% loop $Children %>
   <div>
     <h2>$Title</h2>
     $InLoopForm
   </div>
<% end_loop %>

:

  • getInLoopForm "UserDefinedForm" , Page. YML, .
  • SilverStripe "{PageClassName} _Controller", , . UserDefinedForm , .
  • DataObject, .
+3

SS 4 requires a slight code change:

public function getInLoopForm() {
    if (in_array('SilverStripe\UserForms\Model\UserDefinedForm', $this->ClassAncestry)) {
        $controller = UserDefinedFormController::create($this);
        if ($controller->hasMethod('Form')) {
            return $controller->Form();
        }
    }
    return false;
}
0
source

Source: https://habr.com/ru/post/1625038/


All Articles