Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

problem getting CheckboxSetField data to display in email template


Reply

3 Posts   736 Views

Avatar
Optic Blaze

4 December 2012 at 9:46pm Community Member, 162 Posts

Hi there,

I have a custom email form that i am building and i am having some hassles. I make use of CheckboxSetField for the visitor to select a couple of options. My problem is that when the email template form tries to process the data i get an error message:
Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'ArrayData'

I am guessing that because the $DiveHolidays object is an array and not a string, the template does not like it.

See code below

-------------------------------------------------------------------------------------------
Email form (mysite/code/EnquireForm.php)
--------------------------------------------------------------------------------------------

<?php
class EnquireForm extends Page {   
}

class EnquireForm_Controller extends Page_Controller {
   
   
public static $allowed_actions = array (
   'ContactForm'                           
   );
   
   public function init() {   
      parent::init();
   }
   

   
   function ContactForm() {
      
         // Dive Holidays //
         $holidays = array(   
         'Red Sea North' => 'Red Sea North',
         'Red Sea South' => 'Red Sea South'
         );   
   
// Create fields
$fields = new FieldList(
new TextField('Name', 'Your name and surname*'),
    new TextField('Tel', 'Your contact phone number*'),
new EmailField('Email', 'Your email address*'),
new TextareaField('Comments','Your query/comments*'),
    new CheckboxSetField( 'DiveHolidays', 'Dive Holidays', $holidays)
);

// Create action
$actions = new FieldList(
new FormAction('SendContactForm', 'Submit')
);

// Create Validators
$validator = new RequiredFields('Name', 'Email', 'Comments');
return new Form($this, 'ContactForm', $fields, $actions, $validator);
}
   
    function SendContactForm($data, $form) {
      
$From = 'test@test.com';
$To = 'client@client.com';
$Subject = 'Website Feedback';
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('ContactEmail');
//populate template
$email->populateTemplate($data);
//send mail
$email->send();
//return to submitted message
$form->sessionMessage('We will be in touch','good');
      Controller::curr()->redirectBack();
}

}   

---------------------------------------------------------------
Email template (ContactEmail.ss)
---------------------------------------------------------------

<h1> Your website has generated the following feedback: </h1>

<p>Name: $Name </p>
<p>Contact number: $Tel </p>
<p>Email:$Email </p>
<p>Holidays:$DiveHolidays </p>
<p>Comments:$Comments </p>

Avatar
kewalaka

3 January 2013 at 5:58pm (Last edited: 3 January 2013 5:59pm), Community Member, 1 Post

Hello,

Firstly in your .php I'd suggest removing the spaces from the array IDs, I couldn't get that working:

...
function ContactForm() {

// Dive Holidays //
$holidays = array(
'RedSeaNorth' => 'Red Sea North',
'RedSeaSouth' => 'Red Sea South'
);
...

In your template, ContactEmail.ss, you can display the list of selected items in a CheckboxSetField like this (e.g. using an <ul>):

<ul>
<% control DiveHolidays %> <!-- title of CheckboxSetField -->
   <% if RedSeaNorth %><li>Red Sea North</li><% end_if %>
   <% if RedSeaSouth %><li>Red Sea South</li><% end_if %>
<% end_control %>
</ul>

It is a bit of a cludge but it works for me, does anyone else know a better way?

Stu

Avatar
Optic Blaze

4 January 2013 at 1:08am Community Member, 162 Posts

Hi,

I managed to find a better way. By adding the following, you implode the array and make it a string that you can use in your template:

// Implode checkbox values
if(isset($data['DiveHolidays'])) {$DiveHolidays= implode(',', $data['DiveHolidays']);} else {$DiveHolidays=' ';}

//populate template
$email->populateTemplate($data);
$email->populateTemplate(array('DiveHolidays'=> $DiveHolidays));

Then all you have to do in the .ss template is call
$DiveHolidays