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

时间:2016-06-17 06:36:34

标签: silverstripe

我正在尝试提交在控制器扩展程序中创建的表单。提交后,它会抛出一个错误

enter image description here

可悲的是,我不知道为什么或如何解决这个问题,不会失去验证的构建,所以

我可以手动将表单操作更改为“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);
    }
}

1 个答案:

答案 0 :(得分:5)

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

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

一个简单的解决方法是重写代码,如下所示:

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