Submit form with checkboxes and display selected values in email template

Silverstripe Version:3

Question: I am trying to display selected checkboxes values in an email template but I am getting nowhere. I need some help. For info, the form works and I can display other pieces of data (textfields). Only the checkboxes are giving me trouble.

I have this in my form declaration

new CheckboxSetField(
                    $name = "DataRequired", 
                    $title = "Data Required", 
                    $source = array("1" => "alpha", 
                                    "2" => "beta", 
                                    "3" => "gamma", 
                                    ), $value = "" ),

I tried to display the values in my e,ail template using the following

<tr>
                    <th>Deadline for request</th>
                    <td>$DataRequired</td>
                </tr>

and

<tr>
                    <th>Deadline for request</th>
                    <td><% loop $DataRequired%>$Me<% end_loop %></td>
                </tr>

I know the data is submitted because I can see it in the using debug

dataRequired =
1 =
alpha
2 =
beta
3 =
gamma

Any help would be very appreciated

Me guessing you either didn’t pass the variables in the form of a ArrayData, Or, you haven’t given the email a template for it to use.

Below is a working version:

PHP

<?php

class AcknowledgementEmail extends Email
{

    public function __construct($member) {
        $from     =    Config::inst()->get('Email', 'noreply_email');
        $to       =    $member->Email;
        $subject  =    'Signup Confirmation';

        parent::__construct($from, $to, $subject);

        $this->setTemplate('AcknowledgementEmail');

        $this->populateTemplate(ArrayData::create([
            'MemberID'  =>  $member->ID,
            'baseURL'   =>  Director::absoluteURL(Director::baseURL())
        ]));
    }
}

.ss

(file location: at WWWROOT/mysite/templates/Email/AcknowledgementEmail.ss)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Signup confirmation</title>
</head>
<body>
    <p>Member ID: $MemberID</p>
    <p>base url: $baseURL</p>
</body>
</html>

If you don’t wish to use a customised email (extending Email class) class, then try $email->renderWith(‘dot_ss_template_name’); before you execute ->send();

Cheers
Leo