csy_dot_io
csy_dot_io

Reputation: 1199

Submit form within controller extension - SilverStripe 3.4.0

I'm trying to submit a form created within an controller extension. After Submitting, it throws me an error

enter image description here

Sadly I don't know why or how to solve this, without losing the build in validation and so.

I could manually change the form action to "doSectionForm", than I'll receive the forms data but have lost all the validation.

Here's an excerpt of my code.

<?php
class SectionsPageControllerExtension extends DataExtension {

  private static $allowed_actions = [
    'SectionForm'
  ];

  public function SectionForm($id = null) {
      $fields = FieldList::create(
        HiddenField::create('SectionFormID')
          ->setValue($id)
      );

      $required = RequiredFields::create();
      $actions = FieldList::create(
        FormAction::create('doSectionForm', 'Absenden')
      );

      $form = Form::create($this->owner, 'SectionForm', $fields, $actions, $required);
      // $form->setFormAction($this->owner->Link() . 'doSectionForm');

      return $form;
    }
  }

    public function doSectionForm($data) {
      echo '<pre>';
      print_r($data);
    }
}

Upvotes: 2

Views: 225

Answers (1)

bummzack
bummzack

Reputation: 5875

Actions on controllers usually receive an instance of the SS_HTTPRequest as parameter. This is in conflict with your $id = null parameter. Thus the error-message.

You shouldn't use parameters for your form methods, or if you absolutely need it for the templates, make sure to check if the $id parameter is of type SS_HTTPRequest first (this will be the case when the form is being submitted).

A simple workaround would be to rewrite your code as follows:

$fields = FieldList::create(
    HiddenField::create('SectionFormID')->setValue(
        ($id instanceof SS_HTTPRequest) ? $id->postVar('SectionFormID') : $id
    )
);

Upvotes: 5

Related Questions