Question

For the past couple months I have been pulling hair out trying to get forms to work with Staticpublisher module for Silverstripe.

The form is built properly on the frontend but does not send emails or redirect to the success page when static publisher is enabled.

I have set up the form as follows:

Page.php

    class Page extends SiteTree {

        ............

        public function allPagesToCache() {
                $urls = array();

                $pages = DataObject::get("Page");

                // ignored page types
                $ignored = array(
                    'EnquiryForm'
                );

                foreach($pages as $page) {
                    if (!in_array($page->Classname, $ignored)) {
                        $urls = array_merge($urls, (array)$page->subPagesToCache());    
                    }
                }

                return $urls;
            }

            public function subPagesToCache() {
                $urls = array();
                $urls[] = $this->Link();
                if ($this->ProvideComments) {
                        $urls[] = Director::absoluteBaseURL() . "CommentingController/rss/SiteTree/" . $this->ID;
                }

                return $urls;
            }

            public function pagesAffectedByChanges() {
                $urls = $this->subPagesToCache();
                if($p = $this->Parent) $urls = array_merge((array)$urls, (array)$p->subPagesToCache());
   return $urls;
   }
}

............


class Page_Controller extends ContentController {

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

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

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

EnquiryForm.php

class EnquiryForm extends Form {

public function __construct($controller, $name) {

    // Form fields
    $fields = new FieldList(
        TextField::create('Name')->setAttribute('placeholder', 'Name'),
        EmailField::create('Email')->setAttribute('placeholder', 'Email'),
        TextField::create('Phone')->setAttribute('placeholder', 'Phone'),
        TextareaField::create('Message')->setAttribute('placeholder', 'Message')
    );

    // Strict method check
    $strictFormMethodCheck = true;

    // Form action
    $actions = new FieldList(
        new FormAction('sendEmail', 'Submit')
    );

    // Required fields
    $validator = new RequiredFields('Name', 'Email', 'Phone', 'Message');
    parent::__construct($controller, $name, $fields, $actions, $validator);
}

public function sendEmail($data) {

    // Submit function for contact form above
    if (isset($data) && !empty($data)) {
        // Phone number default 
        $visitorPhone = !empty($data['Phone']) ? $data['Phone'] : '(Not supplied)';

        // Get clients email from the CMS
        $clientEmail = SiteConfig::current_site_config()->Email;

        // Extra emails to send to (CC)
        $ccEmails = SiteConfig::current_site_config()->CCEmails;

        // Setup email
        $email = new Email();
        $email->setTo($clientEmail);
        $email->setCc($ccEmails);
        $email->setFrom($data['Email']);
        $email->setSubject("New website enquiry received from " . ucwords($data["Name"]));

        // Email message
        $messageBody = "
            <h1>Enquiry Via The " . SiteConfig::current_site_config()->Title . " Website</h1>
            <h2>Details</h2>
            <ul>
                <li><strong>Name:</strong> " . $data["Name"] . "</li>
                <li><strong>Email:</strong> <a href='mailto:{$data['Email']}'>{$data['Email']}</a></li>
                <li><strong>Phone:</strong> " . $visitorPhone . "</li>
            </ul>
            <h2>Message</h2>
            <p>{$data['Message']}</p>
        ";

        $email->setBody($messageBody);
        $email->send();

        SESSION::set('Post', $data);

        $customSuccessPage = $this->EnquirySuccessPage()->Link();

        Controller::curr()->redirect($successLink);
    }
}

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

I am then including the form in various templates with {$EnquiryForm}.

I would appreciate any help with this greatly.

Was it helpful?

Solution

Try:

  • Place the sendEmail() function within Page_Controller
  • add 'sendEmail' to the $allowed_actions of the Page_Controller class
  • Replace Controller::curr()->redirect with $this->redirect
  • Within the EnquiryForm function replace: return new EnquiryForm($this, 'EnquiryForm'); with return new EnquiryForm($this, 'sendEmail');

OTHER TIPS

Try updating the allowed_action with the below mentioned code. And also rebuild dev/build the site

private static $allowed_actions = array('EnquiryForm'.'sendEmail');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top