Reputation: 6597
I am trying to figure out the correct way of returning back a form that is pre-populated with data from the database. Let me show you how I am doing it right now:
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($Team.ID)
<a href="home">Back to Home</a>
<% else %>
$GoToLogin()
<% end_if %>
As you can see in my controller I am returning a $Team that I don't fully understand how it works. In my template I use $Team.ID and I am trying to figure out if that value comes from me returning array of Teams or somehow else. The other thing I am trying to understand is why do I not return a Form here, why do I have to have a sepparate function to create that form and have to call it as such in the template. The last problem that I see here is that I am querying the database twice for the same information.
What I am asking in the end is If anyone could show me what would be the proper way to achieve what I am doing. I'm sure it has been done many times, but I could not find a single example anywhere on the internet of how to use edit forms. I have found how to use show action and return bunch of results, but nothing in combination with the forms.
Upvotes: 0
Views: 446
Reputation: 619
I suggest simply doing the following (these are additions to your code, not a complete class):
class TeamPage_Controller extends Page_Controller {
protected $currentTeam;
protected function getCurrentTeam() {
if (!isset($this->currentTeam)) {
$teamID = $this->getRequest()->param('ID') ?: $this->getRequest()->postVar('ID');
$this->currentTeam = $teamID ? Team::get()->byID($teamID) : null;
}
return $this->currentTeam;
}
public function EditTeamForm() {
$team = $this->getCurrentTeam();
// You'll want to add a HiddenField with the ID if $team is not null
// everything else is the same
}
// NOTE: I would suggest you call this something else
// SS convention would be "doSaveTeam" - it should be whatever your FormAction is called though
public function EditTeam($data, $form) {
$team = $this->getCurrentTeam();
// Save and redirect
}
public function edit(SS_HTTPRequest $request){
$team = $this->getCurrentTeam();
// Blah blah blah
}
}
Upvotes: 1