在控制器扩展中提交表单 - SilverStripe 3.4.0

Submit form within controller extension - SilverStripe 3.4.0

我正在尝试提交在控制器扩展中创建的表单。提交后报错

遗憾的是,我不知道为什么或如何解决这个问题,不会丢失构建验证等

我可以手动将表单操作更改为 "doSectionForm",这样我将收到表单数据但已丢失所有验证。

这是我的代码的摘录。

<?php
class SectionsPageControllerExtension extends DataExtension {

  private static $allowed_actions = [
    'SectionForm'
  ];

  public function SectionForm($id = null) {
      $fields = FieldList::create(
        HiddenField::create('SectionFormID')
          ->setValue($id)
      );

      $required = RequiredFields::create();
      $actions = FieldList::create(
        FormAction::create('doSectionForm', 'Absenden')
      );

      $form = Form::create($this->owner, 'SectionForm', $fields, $actions, $required);
      // $form->setFormAction($this->owner->Link() . 'doSectionForm');

      return $form;
    }
  }

    public function doSectionForm($data) {
      echo '<pre>';
      print_r($data);
    }
}

控制器上的操作通常接收 SS_HTTPRequest 的实例作为参数。这与您的 $id = null 参数冲突。因此错误消息。

你不应该为你的表单方法使用参数,或者如果你绝对需要它用于模板,请确保首先检查 $id 参数是否为 SS_HTTPRequest 类型(这将提交表格时就是这种情况)。

一个简单的解决方法是按如下方式重写您的代码:

$fields = FieldList::create(
    HiddenField::create('SectionFormID')->setValue(
        ($id instanceof SS_HTTPRequest) ? $id->postVar('SectionFormID') : $id
    )
);