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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Prepopulating form with data from database


Go to End


858 Views

Avatar
Bagzli

Community Member, 71 Posts

21 September 2015 at 2:26pm

Edited: 21/09/2015 2:27pm

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 method 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 %>