user3574603
user3574603

Reputation: 3628

SilverStripe: Trying to implement Contact Form function but getting white screen of death

I'm following this tutorial

I have a page called 'Contact.ss'. The php file looks like this:

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);
    }
}

But when I call $ContactForm in the template I get a blank screen when I try to load the page. (500 error)

I've checked to see if it's possible to call the function from the template by replacing all the ContactForm()'s code with:

return "Hello, World!"

It works, so I know the function is being called. But I can't see what's wrong with the code from the tutorial.

Can anyone help me out?

Upvotes: 1

Views: 569

Answers (1)

3dgoo
3dgoo

Reputation: 15804

The issue is the tutorial you have used is written for SilverStripe 2.4 while you are using a newer version, SilverStripe 3.1.

For SilverStripe 3.1 I suggest going through the SilverStripe Frontend Forms lesson rather than the SSBits tutorial. The SSBits tutorial is from 2010 and is for SilverStripe 2.4. The SilverStripe Frontend Forms lesson is from 2015 and is for the current version of SilverStripe.

With your current code there are a number of bits of code that need to be updated to work in the latest version of SilverStripe.

FieldSet has been replaced by FieldList. You will need to replace each instance of FieldSet with FieldList in your code.

Your ContactForm should look more like this:

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);
}

In SilverStripe 3.1 the in built static variables need to be declared private.

Make sure you declare your $allowed_actions as private:

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

As well as your $db as private:

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

Upvotes: 1

Related Questions