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.

Form Questions /

HasManyComplexTableField - can't get it to work


Reply


2 Posts   599 Views

Avatar
ScottT

Community Member, 9 Posts

14 April 2012 at 7:59am

Hi folks,
I am brand new to SS, so please bear with me. I am trying to set up a front-end form. I have a Model SiennaClient with 1-many relationship to Model SiennaAddress. Both extend DataObject. My front-end form model SiennaClientTestForm (extends Page) and SiennaClientTestForm_Controller (extends Page_Controller).

I have the form working basically with a variety of FormField types. I need to be able to enter possibly multiple addresses for each client (people move). I believe a HasManyComplexTableField is the way to go, however, I cannot get it working. I can get ComplexTableField to work fine, but it only allows me to enter 1 address.

Here's the call to the HasManyComplexTableFieldConstructor:
new HasManyComplexTableField($this, 'Addresses', 'SiennaAddress', array('Name'=>'Address Name') ),

When I try to view the form, I get a fatal error:
Fatal error: Call to a member function Addresses() on a non-object in /var/www/silverstripe/sapphire/forms/HasManyComplexTableField.php on line 117

Here's some of the relevant code:
SiennaAddress.php:
class SiennaAddress extends DataObject {
...
public static $has_one = array(
         'SiennaClientTestForm' => 'SiennaClientTestForm',
         );
}

SiennaClientTestForm.php:
class SiennaClientTestForm extends Page {
public static $has_one = array (
         'Client' => 'SiennaClient'
         );
   
   public static $has_many = array (
         'Addresses' => 'SiennaAddress'
         );

}

class SiennaClientTestForm_Controller extends Page_Controller {

$form = new Form(
...
new HasManyComplexTableField($this, 'Addresses', 'SiennaAddress', array('Name'=>'Address Name') )
);
....
return $form;
}

At this point, I am completely stuck. I have read the APIs, Tutorials, Docs, etc. but cannot find the source of the problem. Also, the documentation on HasManyComplexTableField is sparse.

Please help.

thanks,
Scott

Avatar
jegelstaff

Community Member, 2 Posts

14 April 2012 at 9:10am

I work with Scott, and we narrowed down the issue to this line in HasManyComplexTableField.php:

$selectedItems = $this->form->getRecord()->$fieldName();

The problem seems to be fixed, or at least surpressed, by changing that line to this:

$currentRecord = $this->form->getRecord();
if($currentRecord) {
$selectedItems = $currentRecord->$fieldName();
} else {
return array();
}

This seems to make sense, because when the form is first loading on screen and it's empty, there are no selectedItems to gather. So it makes sense that an empty record would be returned and there would be no fields of the right name in an empty record!

So I wonder if this is a core bug in Sapphire, that it should account for the possibility of an empty record there, in something like the way I've done with that code? Comments from those more knowledgable about Sapphire would certainly be appreciated. Thanks!

--Julian