0
votes

I am using the MultiForm module to submit a long form with SilverStripe. The logic for this form is in 'CampaignBriefForm.php' whereas the gridfield CMS field is being added in 'CampaignBriefPage.php'. I have a Data Object for a CampaignBriefLead which is what the form creates.

Campaign Brief Page

private static $has_many = array(
    'CampaignBriefLeads'    =>  'CampaignBriefLead'
);

public function CampaignBriefForm() {
    return new CampaignBriefForm($this, 'CampaignBriefForm');
}

Campaign Brief Lead (DO)

private static $has_one = array( "Page" => "CampaignBriefPage" );

As you can see the Campaign Brief page has the correct relationship with the Data Object and also you can see the the form itself (done in a sepearate file) is correctly returning (as it's being saved in the DB). For some reason however, the gridfield will not show me what is in the database for that Data Object. The grid field code is as follows.

$fields = parent::getCMSFields();
    $contactConfig = GridFieldConfig_RelationEditor::create();
    $contactConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(
        array(
            'CompanyName'   => 'Company Name',
            'StartDate'     => 'Start Date',
            'Duration'      => 'Duration',
            'WebsiteURL'    => 'Website',
            'Budget'        => 'Budget'
    )); 

    $contactGrid = new GridField(
        'CampaignBrief',
        'Campaign Enquiries',
        $this->CampaignBriefLeads(),
        $contactConfig
    );

    $fields->addFieldToTab("Root.Enquiries", $contactGrid);

To me this all looks correct and should work but for some reason it is not working.

Note

The link existing option on the gridfield allows me to link one of the entries from the DO with the gridfield weirdly?? So it saves one entry but I have to do it manually, this tells me it can see the DB but won't pull for some reason.

For reviewing reasons, here is the code for the multiform where the campaign brief lead is actually saved to the DB after the form is submitted.

public function finish($data, $form) {

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

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

    $enquiry = new CampaignBriefLead();


    foreach($steps as $step) {
        $data = $step->loadData();

        foreach($data as $key => $value) {
            if($key == 'url' || $key == 'MultiFormSessionID' || $key == 'action_finish') {
                continue;
            }
            if(isset($data[$key])) {
                $enquiry->$key = $data[$key];
                error_log($data[$key]);
            }
        }
    }

    $enquiry->write();

    $this->controller->redirect('/campaign-brief/');
}

If you need anything more let me know. Thanks.

1

1 Answers

1
votes

I would take a guess that the CampaignBriefLead PageID is not being set on your form submission.

Check the CampaignBriefLead table in your database and check the PageID column. If it is blank, null or 0 for each row then it is not being set.

One way to fix this problem for any new submission is to set the PageID for the $enquiry:

public function finish($data, $form) {

    // ...

    $enquiry = new CampaignBriefLead();

    if ($campaignBriefPage = CampaignBriefPage::get()->first()) {
        $enquiry->PageID = $campaignBriefPage->ID;
    }

    // ...
}

For the existing entries you will need to update the entries to have the correct PageID.