Peter Harley Broom
Peter Harley Broom

Reputation: 115

SilverStripe - Using the CheckboxsetField with MultiForm Module

I have a form using the multiform module. I have a checkboxsetfield populated by a dataobject.

When saving the form I am getting strange results. For instance if I select the first and third checkboxes then this is how the array appears in the database: 1{comma}3 when I expected to see 1,3

MyDataObject.php

<?php
...
       if($SomeData = DataObject::get('SomeData')->sort('SortColumn'){
        $fields->push( new CheckboxSetField('SomeData', 'Field Name', $SomeData->map('ID', 'Name')
        ));
    }

MultiForm.php

<?php
...

public function finish($data, $form){

        if(isset($_SESSION['FormInfo']['MultiForm']['errors'])){
            unset($_SESSION['FormInfo']['Form']['errors']);
        }

        parent::finish($data, $form);

        $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");

        $MyStep = $this->getSavedStepByClass('MyStep');

        if($this->getSavedStepByClass('MyStep')){
            if($MyStep->loadData()){
                $MyDataObject = new MyDataObject();
                $MyStep->saveInto($MyDataObject);
                $MyDataObject->write();
            }
        }
...

Any ideas how to process the array?

Upvotes: 3

Views: 454

Answers (1)

Turnerj
Turnerj

Reputation: 4278

CheckboxSetField does have code which refers to {comma} when saving to the DB or when calling the dataValue function. This is essentially escaping any commas that were defined as values in the string when saving to a single column.

This tells me that either your multiform isn't providing the right input to CheckboxSetField or that there is more to this situation than your code is showing.

If CheckboxSetField gets an array like array('1,3'), that is when I would expect to see that type of result. Calling map like you have returns an SS_Map object which may not automatically convert the way you are expecting. Try adding ->toArray() after the map call when you are passing the values into the CheckboxSetField.

If that doesn't solve the issue, we probably will need to see the DataObject itself and a few other bits and pieces of information.

Upvotes: 1

Related Questions