Bagzli
Bagzli

Reputation: 6597

Silverstripe populate form based on url

I have TeamsPage class and Team class. I am trying to figure out how to pre-populate the form with the data from the database based on the ID that was passed in the URL. Below is the code of my attempt, I tried to pass in the ID via template but that did not work. How else can I accomplish this? I would prefer if there was a way to pass the team as an object that I already have in the edit function so that I don't have to hit the database twice. Is there a way to do this?

TeamsPage:

<?php
class TeamsPage extends Page {
  private static $has_many = array (
    'Teams' => 'Team',
  );
  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Teams', GridField::create(
        'Teams',
        'Teams on this page',
        $this->Teams(),
        GridFieldConfig_RecordEditor::create()
    ));
    return $fields;
  }
}
class TeamsPage_Controller extends Page_Controller {
  private static $allowed_actions = array (
    'show', 'edit', 'EditTeamForm'
  );

  public function EditTeamForm($teamId){
    $fields = new FieldList(
        new TextField('TeamName'),
        new TextareaField('TeamDescription')
    );
    $actions = new FieldList(
        new FormAction('EditTeam', 'Save Changes')
    );
    $requiredFields = new RequiredFields(array('TeamName','TeamDescription'));
    $form = new Form($this, 'EditTeamForm', $fields, $actions, $requiredFields);
    $form->setFormMethod('POST', true);

    $data = Session::get("FormData.{$form->getName()}.data");
    $team = Team::get()->byID($teamId);
    return $data ? $form->loadDataFrom($data) : $form->loadDataFrom($team);
  }

  public function show(SS_HTTPRequest $request) {
    $team = Team::get()->byID($request->param('ID'));

    if(!$team) {
        return $this->httpError(404,'That team could not be found');
    }

    return array (
        'Team' => $team
    );
  }

  public function edit(SS_HTTPRequest $request){
    $team = Team::get()->byID($request->param('ID'));

    if(!$team) {
        return $this->httpError(404,'That team could not be found');
    }

    return array (
        'Team' => $team
    );
  }
}

Team:

<?php
class Team extends DataObject {
  private static $db = array(
    'TeamCaptain' => 'Int',
    'TeamName' => 'Varchar',
    'TeamDescription' => 'Text'
  );
  private static $has_one = array (
    'Photo' => 'Image',
    'TeamsPage' => 'TeamsPage'
  );
  private static $summary_fields = array (
    'GridThumbnail' => '',
    'TeamCaptain' => 'Team Captain',
    'TeamName' => 'TeamName',
    'TeamDescription' => 'Team Description',
  );
  public function getGridThumbnail() {
    if($this->Photo()->exists()) {
        return $this->Photo()->SetWidth(100);
    }
    return '(no image)';
  }
  public function getCMSFields() {
    $fields = FieldList::create(
        TextField::create('TeamCaptain'),
        TextField::create('TeamName'),
        TextareaField::create('TeamDescription'),
        $uploader = UploadField::create('Photo')
    );
    $uploader->setFolderName('teams-photos');
    $uploader->getValidator()->setAllowedExtensions(array(
        'png','gif','jpeg','jpg'
    ));
    return $fields;
  }

  public function Link() {
    return $this->TeamsPage()->Link('show/'.$this->ID);
  }
}

TeamsPage_edit.ss

<% if GetMember() %>
  Welcome $getMember.FirstName<br />
  $EditTeamForm($ID)
  <a href="home">Back to Home</a>
<% else %>
  $GoToLogin()
<% end_if %>

Upvotes: 1

Views: 283

Answers (1)

Mark Guinn
Mark Guinn

Reputation: 619

It looks to me like you're passing the wrong ID to EditTeamForm from the template. Unless there is a <% with %> statement that I'm not seeing I think you want to call:

$EditTeamForm($Team.ID)

Everything else looks fine to me.

Upvotes: 2

Related Questions