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.

Form Questions /

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

Form not saving data


Go to End


1694 Views

Avatar
Bagzli

Community Member, 71 Posts

27 September 2015 at 4:30pm

Hi,

I am using DataObject as Pages direction to create a simple Create Teams, Edit Teams, Delete Teams functionality. So I as a user should be able to create a team, edit it, view it and delete it. When I go to www.mywebsite.com/teams/edit/1 the form will show and it will pre-populate the data from the database, however when I make a change and hit Save, it comes with back message "Team has not been saved" If you look at the code you will see that I am also trying to print out the ID of the team, but that does not gets printed out. For some reason, it cannot retrieve the ID it is suppose to be updating and there fore the if/else in doEditTeam function it returns that message.

Have I missed something?

Right now I have the view portion working without problems, however when I do the edit form, the data will not save. Here is the code:

<?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);
    }
}

<?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 {
    protected $currentTeam;

    private static $allowed_actions = array (
        'show', 'edit', 'EditTeamForm'
    );

    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(){
        $fields = new FieldList(
            new HiddenField('ID'),
            new TextField('TeamName'),
            new TextareaField('TeamDescription')
        );
        $actions = new FieldList(
            new FormAction('doEditTeam', '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 = $this->getCurrentTeam();
        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 = $this->getCurrentTeam();
        SS_Log::log($team->ID . " loaded", SS_Log::WARN);
        if(!$team) {
            return $this->httpError(404,'That team could not be found');
        }

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

    function doEditTeam($data, $form){
        Session::set("FormData.{$form->getName()}.data", $data);
        $team = $this->getCurrentTeam();
        $team = Team::get()->byID($data['ID']);
        if($team){
            $form->saveInto($team); 
            $team->write();
            Session::clear("FormData.{$form->getName()}.data");
            SS_Log::log($team->ID . " updated", SS_Log::WARN);
            $form->sessionMessage("Team has been updated!", 'bad');
            return $this->redirectBack();
        }
        else{
            $form->sessionMessage("Team has not been updated " . $this->getRequest()->postVar('ID') . $this->getRequest()->param('ID'), 'bad');
            return $this->redirectBack();
            //return $this->redirect('../Home');
        }
    }
}