【问题标题】:SilverStripe: Trying to implement Contact Form function but getting white screen of deathSilverStripe:尝试实现联系表单功能但出现白屏死机
【发布时间】:2015-05-20 15:12:35
【问题描述】:

我正在关注这个tutorial

我有一个名为“Contact.ss”的页面。 php 文件如下所示:

class Contact extends Page {

    private static $has_one = array (
        'Photograph' => 'Image'
    );

    static $db = array (
        'MailTo' => 'Varchar(100)',
        'SubmitText' => 'Text'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Main', $Photograph = UploadField::create('Photograph'), 'Content');
        $Photograph->getValidator()->setAllowedExtensions(array('png','jpeg','jpg','gif'));
        $Photograph->setFolderName('photographs');

        $fields->addFieldToTab("Root.Main", new Textfield('Mailto', 'Address to email contact form submission to'));
        $fields->addFieldToTab("Root.Main", new TextareaField('SubmiteText', 'Text displayed after contact form submission'));

        return $fields;
    }

}

class Contact_Controller extends Page_Controller {
    static $allowed_actions = array(
        'ContactForm'
    );

    function ContactForm() {
        // Create fields
        $fields = new FieldSet(
            new TextField('Name', 'Name*'),
            new EmailField('Email', 'Email*'),
            new TextareaField('Comments','Comments*')
        );

        // Create action
        $actions = new FieldSet(
            new FormAction('SendContactForm', 'Send')
        );

        // Create Validators
        $validator = new RequiredFields('Name', 'Email', 'Comments');

        return new Form($this, 'ContactForm', $fields, $actions, $validator);
    }
}

但是当我在模板中调用$ContactForm 时,当我尝试加载页面时会出现空白屏幕。 (500 错误)

我已检查是否可以通过将所有ContactForm() 的代码替换为:

return "Hello, World!"

它有效,所以我知道该函数正在被调用。但我看不出教程中的代码有什么问题。

谁能帮帮我?

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    问题是您使用的教程是为 SilverStripe 2.4 编写的,而您使用的是较新的版本 SilverStripe 3.1。

    对于 SilverStripe 3.1,我建议阅读 SilverStripe Frontend Forms lesson 而不是 SSBits 教程。 SSBits 教程来自 2010 年,适用于 SilverStripe 2.4。 SilverStripe 前端表单课程从 2015 年开始,适用于当前版本的 SilverStripe。

    对于您当前的代码,有许多代码需要更新才能在最新版本的 SilverStripe 中工作。

    FieldSet 已替换为 FieldList。您需要将代码中的每个FieldSet 实例替换为FieldList

    您的ContactForm 应该看起来更像这样:

    function ContactForm() {
        // Create fields
        $fields = FieldList::create(
            TextField::create('Name', 'Name*'),
            EmailField::create('Email', 'Email*'),
            TextareaField::create('Comments','Comments*')
        );
    
        // Create action
        $actions = FieldList::create(
            FormAction::create('SendContactForm', 'Send')
        );
    
        // Create Validators
        $validator = RequiredFields::create('Name', 'Email', 'Comments');
    
        return Form::create($this, 'ContactForm', $fields, $actions, $validator);
    }
    

    在 SilverStripe 3.1 中,内置静态变量需要声明为私有。

    确保将您的 $allowed_actions 声明为私有:

    private static $allowed_actions = array(
        'ContactForm'
    );
    

    以及您的$db 为私有:

    private static $db = array (
        'MailTo' => 'Varchar(100)',
        'SubmitText' => 'Text'
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2012-05-12
      • 2022-10-19
      相关资源
      最近更新 更多