本文介绍了Silverstripe通过控制器返回填充的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出正确的方法,以返回用数据库中的数据预先填充的表单。让我告诉你我现在如何做:

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:

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

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,我试图确定该值是否来自我返回的Teams数组或其他原因。我试图理解的另一件事是为什么我不在这里返回Form,为什么我必须要有一个单独的函数来创建该Form并必须在模板中这样调用它。我在这里看到的最后一个问题是我在数据库中两次查询了相同的信息。

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.

最后我要问的是,如果有人可以告诉我什么呢?是实现我正在做的事情的正确方法。我敢肯定它已经做过很多次了,但是我在互联网上的任何地方都找不到如何使用编辑表单的例子。我已经找到了如何使用show action和返回结果的方法,但是没有与表单结合使用的方法。

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.

推荐答案

我建议执行以下操作(这些是对代码的添加,而不是完整的类):

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

这篇关于Silverstripe通过控制器返回填充的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:36