【问题标题】:Silverstripe 3.1 action for custom form not executed未执行自定义表单的 Silverstripe 3.1 操作
【发布时间】:2014-03-18 16:32:58
【问题描述】:

我有一个名为 ForgotPasswordPage.php 的自定义页面和一个 ForgotPasswordPage.ss 模板。我在 ForgotPasswordForm.php 中还有一个自定义表单类,它是相应的自定义表单模板 ForgotPasswordForm.ss,位于模板/包含目录中。 表单操作应该调用 doForgotPassword,但永远不会调用此函数,否则,我会被发送到 google.com。 这似乎非常基本,但我有两个开发人员正在查看它,我们得到的只是以下错误:

似乎出现了技术问题。请点击返回按钮,刷新浏览器,然后重试。

我在这里做错了什么?

ForgotPasswordForm.php

<?php

class ForgotPasswordForm extends Form { 
    function __construct($controller, $name, $arguments=array()) { 
        $fields = new FieldList( 
            EmailField::create("Email") 
        ); 
        $actions = new FieldList(FormAction::create("doForgotPassword")->setTitle("RETRIEVE PASSWORD")); 
        $validator = new RequiredFields('Email'); 
        parent::__construct($controller, $name, $fields, $actions, $validator); 
    }

    public function doForgotPassword($data, Form $form) { 
        //As a test, if we ever get here, the controller should send me to the Google website
        Controller::curr()->redirect('http://www.google.com'); 
    }

    public function forTemplate() { 
        return $this->renderWith(array( 
            $this->class, 
            'Form' 
        )); 
    }

}

ForgotPasswordForm.ss

<form $FormAttributes> 
    <label for="{$FormName}_Email">Enter Your Email Address</label> 
    $Fields.dataFieldByName(Email) 
    <% if $Actions %> 
        <% loop $Actions %> 
            $Field 
        <% end_loop %> 
    <% end_if %> 
</form>

ForgotPasswordPage.php

class ForgotPasswordPage extends Page { 
. 
. 
. 
}

class ForgotPasswordPage_Controller extends Page_Controller {

    public static $allowed_actions = array ( 
        'MyForgotPasswordForm' 
    );

    public function init() { 
        parent::init(); 
    }

    public function MyForgotPasswordForm(){ 
        return new ForgotPasswordForm($this, 'MyForgotPasswordForm'); 
    }

}

ForgotPasswordPage.ss

. 
. 
. 
$MyForgotPasswordForm 
. 
. 
.

【问题讨论】:

    标签: php forms silverstripe


    【解决方案1】:

    为了保护表单免受 xsrf 攻击,silverstripe 表单通常构建有一个额外的隐藏字段,其中填充了一个安全令牌,在提交时会对其进行检查。通过重写表单的模板文件,不再包含此标记。您可以通过在 ForgotPasswordForm.ss 中的 $Fields.dataFieldByName(Email) 之后添加 $Fields.dataFieldByName(SecurityID) 来包含它。或者,您可以遍历字段,这是一种更强大的解决方案(这是 Form.ss 中的方法)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2017-11-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多