Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » problem getting CheckboxSetField data to display in email template

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 723 Views
  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    problem getting CheckboxSetField data to display in email template Link to this post

    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>

  • kewalaka
    Avatar
    Community Member
    1 Post

    Re: problem getting CheckboxSetField data to display in email template Link to this 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

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: problem getting CheckboxSetField data to display in email template Link to this post

    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

    723 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.