【问题标题】:SilverStripe Form Redirecting ErrorSilverStripe 表单重定向错误
【发布时间】:2016-07-26 03:30:48
【问题描述】:

如果我单击提交按钮将数据从我的SilverStripe 表单发送到数据库 (xampp, mysql),它不会将我重定向回表单页面。相反,它重定向到{BaseUrl}name_of_controller/name_of_form -> 在我的情况下:localhost/pmtool/DeveloperController/NewTaskMask。我已经搜索了几个小时来解决这个问题,但没有任何成功。

这是文件的代码,其中定义了表单和操作。 在另一个 html 文件中,表单将通过$NewTaskMask 调用。通过 URL localhost/pmtool/developer/newtask 将调用函数renderNewTaskForm,将呈现带有$NewTaskMask 的模板。

数据库中确实存在标题、描述等所有字段。

class DeveloperController extends ContentController {

    private static $allowed_actions = array('NewTaskMask','renderNewTaskForm');

    private static $url_handlers = array(
        'newtask' => 'renderNewTaskForm'
    );

    public function renderNewTaskForm(SS_HTTPRequest $request) {
        return $this->renderWith('NewTaskForm');
    }

    public function NewTaskMask() {
        $fields = new FieldList(
            TextField::create('Title','Title')
                ->setAttribute('autocomplete', 'off')
                ->setAttribute('placeholder', 'Enter a task title ...'),
            TextareaField::create('Description','Description')
                ->setAttribute('autocomplete', 'off'),
            DropdownField::create('Project','Project', Project::get()->map('ID','Title')),
            DropdownField::create('Developer','Developer', Developer::get()->map('ID','Title'))
        );

        $actions = new FieldList(FormAction::create('doCreateT','Task anlegen'));

        $requiredFields = new RequiredFields(array('Title'));

        return new Form($this, 'NewTaskMask', $fields, $actions, $requiredFields);
    }

    public function doCreateT($data, $form) {

        // Creating a new task record
        $nT = new Task();

        $nT->Title = $data['Title'];
        $nT->Description = $data['Description'];
        $nT->Project = $data['ProjectID'];
        $nT->Developer = $data['DeveloperID'];
        $nT->write();

        // Create a nice msg for our users
        $form->sessionMessage('Task angelegt!','good');

        // Redirect back to the form page
        return $this->redirectBack();
    }
}

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    似乎是路由问题。我假设你已经设置了developer 路由来访问DveloperController。做同样的事情我可以重现你的错误。

    我尝试为DeveloperController 显式添加另一条路由以访问DeveloperController 控制器,因为它没有正确重定向。这是一个非常奇怪和神奇的修复,老实说我不确定它为什么有效,但它解决了它:

    ---
    Name: mysiteroutes
    After: framework/routes#coreroutes
    ---
    Director:
      rules:
        'developer//$Action/$ID/$Name': 'DeveloperController'
        'DeveloperController//$Action/$ID/$Name': 'DeveloperController'
    

    更新路线以与您的设置相对应,之后不要忘记运行?flush

    也许有更深入的人可以解释发生了什么。

    【讨论】:

    • 感谢 Janne 您的回答,但是:它现在重定向回来,好的,但不会调用表单 ("doCreateT") 的操作。当你放一些回声(“你好”)时,你可以检查这个;动作中的命令。由于无法达到操作的问题,也没有创建数据库条目
    • 嗯,这很奇怪。它适用于我运行this code
    • 现在可以了!你的解决方案是正确的。非常感谢!但是我还有一个问题,看下一个答案
    【解决方案2】:

    我想用模板渲染页面“localhost/pmtool/developer/$ID”。 所以我添加了 '$ID!' => 'index' 到 url_handlers。但是现在和开始时一样的问题。我是否必须为这个问题添加另一条路线或有 '$ID!' 行=> 'index' 一个错误?

    private static $url_handlers = array(
        'newtask' => 'renderNewTaskForm',
        '$ID!' => 'index'
    );
    
    public function index(SS_HTTPRequest $request) {
            return $this->renderWith('DeveloperTemplate');
    }
    

    【讨论】:

    • 您已经定义了不能与developer//$ID! 共存的路由developer//$Action/$ID/$Name。但是默认情况下所有请求都会命中索引任务,因此您应该可以省略'$ID!$ => 'index',然后在索引函数中您可以通过执行$request->param('ID')来获取参数。
    • 直到现在我还没有看到你的答案。问题已解决。创建了一个新的控制器来处理新的路由“localhost/pmtool/developer/projects/$ID”。现在索引函数将由新控制器处理。但是谢谢你的回答!我认为您的解决方案会是一个更快更好的解决方案:D
    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2013-07-31
    • 2013-08-08
    • 2013-03-24
    相关资源
    最近更新 更多