Stefan Orr
Stefan Orr

Reputation: 15

Editing submitted form data in Silverstripe

So I want to be able to have a page that is only available to the admin of the site that lists a summary of submitted forms and links to them. So once you click on the summary it takes you to an identical form that lets you edit/update the form. I have everything working except the updating part and I'm stuck.

SubmitApplication works correctly:

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

Listing applications in page only available to admin:

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

View application form for updating:

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
}

Upvotes: 0

Views: 167

Answers (1)

Gavin Bruce
Gavin Bruce

Reputation: 1809

The doUpdateApplication function needs to know what record to update so you can use a hidden field.

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

Then you can use this id to determine which record to update.

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

Upvotes: 1

Related Questions