Silverstripe通过控制器返回填充的表单

时间:2015-09-23 14:21:07

标签: forms controller silverstripe

我试图弄清楚返回一个预先填充了数据库中数据的表单的正确方法。让我告诉你我现在是怎么做的:

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

小组:

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

正如您在我的控制器中看到的那样,我将返回一个$ Team,我不完全了解它是如何工作的。在我的模板中,我使用$ Team.ID,我试图弄清楚这个值是否来自我返回的团队数组或其他方式。我想要理解的另一件事是为什么我不在这里返回一个表单,为什么我必须有一个sepparate函数来创建该表单,并且必须在模板中调用它。我在这里看到的最后一个问题是我正在查询数据库两次以获取相同的信息。

我最后要问的是,如果有人能告诉我什么是实现我正在做的事情的正确方法。我确信它已经完成了很多次,但我无法在互联网上的任何地方找到如何使用编辑表单的单一示例。我已经找到了如何使用show action并返回一堆结果,但没有与表单结合使用。

1 个答案:

答案 0 :(得分:1)

我建议简单地执行以下操作(这些是对代码的补充,而不是完整的类):

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
    }
}