在Silverstripe中编辑提交的表单数据

时间:2018-03-26 04:33:19

标签: silverstripe silverstripe-4

因此,我希望能够拥有一个页面,该页面仅供网站管理员使用,该网页列出了已提交表单的摘要及其链接。因此,一旦您点击摘要,它将转到相同的表单,您可以编辑/更新表单。除了更新部分之外,我的一切都在工作,而且我被卡住了。

SubmitApplication正常运行:

class SubmitApplicationPageController extends PageController{
    protected function init()
    {
        parent::init();
    }

    private static $allowed_actions = [
        'ApplicationForm'
    ];

    public function ApplicationForm()
    {
        $fields = new FieldList(
            TextField::create('First_Name')->setTitle('First Name'),
            TextField::create('Last_Name')->setTitle('Last Name')
        );

        $actions = new FieldList(
            FormAction::create('doSubmitApplication')->setTitle('Submit')
        );

        $validator = new RequiredFields([
            'First Name',
            'Last Name',
        ]);

        return new Form($this, 'ApplicationForm', $fields, $actions, $validator);
    }

    public function doSubmitApplication($data, Form $form)
    {
        $submission = new Application();
        $form->saveInto($submission);
        $submission->write();
        $form->sessionMessage('Thank you for your submission we will get back to you as soon as possible', 'success');
        return $this->redirectBack();
    } 
}

在页面中列出仅供管理员使用的应用程序:

<ul>
  <% loop $Applications %>
    <li>$First_Name $Last_Name <a href="view-application/?id=$ID">View Application</a></li>
  <% end_loop %>
</ul>

查看更新申请表:

private static $allowed_actions = [
    'GetApplicationForm'
];

public function GetApplicationForm(){
    $var = $this->getRequest()->getVar('id');

    if($var){
        $fields = new FieldList(
            TextField::create('First_Name')->setTitle('First Name'),
            TextField::create('Last_Name')->setTitle('Last Name')
        );

        $actions = new FieldList(
            FormAction::create('doUpdateApplication')->setTitle('Update')
        );

        $validator = new RequiredFields([
            'First Name',
            'Last Name'
        ]);

        $form = Form::create($this, 'GetApplicationForm', $fields, $actions, $validator)->loadDataFrom(Application::get()->filter(['ID' => $var])[0]);

        return $form;
    }
    return 'This page should only be reached through the application management links. If you are here even though you did that, please contact your system admin.';
}

public function doUpdateApplication($data, Form $form)
{
    //I can't figure this part out and clicking reruns the GetApplicationForm method without the get variable and doesn't run this method
}

1 个答案:

答案 0 :(得分:1)

doUpdateApplication函数需要知道要更新的记录,以便您可以使用隐藏字段。

$fields = new FieldList(
    HiddenField::create('id'),
    TextField::create('First_Name')->setTitle('First Name'),
    TextField::create('Last_Name')->setTitle('Last Name')
);

然后,您可以使用此id来确定要更新的记录。

public function doUpdateApplication($data, Form $form) {
    $submission = DataList::create('Application')->byId($data['id']);
    $form->saveInto($submission);
    $submission->write();
    //the rest of your code
}
相关问题