0

I have a Form in my custom PageController.

The Form renders properly in template Register.ss.
In my setup the submit - function on PageController doesn't work.

I have tried it without setting the submit-route in routes.yml resulting in a 404.

I have tried to set the submit-route in routes.yml and in $url_handlers which results in an error:

Uncaught ArgumentCountError: Too few arguments to function TherapyRegisterController::submit(), 1 passed in /var/home/xxxxx/vendor/silverstripe/framework/src/Control/RequestHandler.php on line 323 and exactly 2 expected

How to get the submit - function to work?

//routes:

SilverStripe\Control\Director:
  rules:
    therapy//anmeldung/$ID: TherapyRegisterController
#   therapy//submit: TherapyRegisterController

TherapyRegisterController:

class TherapyRegisterController extends PageController{

    private static $allowed_actions = ['registerForm', 'submit'];
    
    
    private static $url_handlers = [
        'anmeldung/$ID' => 'index',
        //'anmeldung/submit' => 'submit',
    ];
    

    public function registerForm($id)
    {

        $fields = new FieldList( 
            TextField::create('Name', 'Name')
        );
        
        $actions = new FieldList( [
            $cancelButton = FormAction::create('cancel')->setTitle('ABBRECHEN')->setAttribute('formaction', 'therapy/cancel'), // 'cancel'
            $sendButton = FormAction::create('submit')->setTitle('ANMELDEN')->setAttribute('formaction', 'therapy/submit')     // 'submit'
        ]);
        
        $validator = new RequiredFields('');
        $form = new Form($this, 'registerForm', $fields, $actions, $validator);
        $form->setTemplate('Register');
        
        return $form;
    }
    
    
    public function submit($data, Form $form)
    {
        Debug::show($data);
    }
    
    
    public function index(HTTPRequest $request)
    {
        
        $arrayData = array (
            'ID' => $request->param('ID')
        );
        return $this->customise($arrayData)->renderWith(array('Anmeldung', 'Page'));
    }
    
    

Register.ss :

    $registerForm($ID)
Sepp Hofer
  • 217
  • 2
  • 12

1 Answers1

0

The ArgumentCountError does say that the submit method was only getting 1 argument but that submit method is expecting 2 as seen in your code. I'm not sure what exact version of SilverStripe you have but I can see this on line 323 of that RequestHandler.php:

$actionRes = $this->$action($request);

That first argument is going to be a SilverStripe\Control\HTTPRequest. The form submission should be a POST. You can get that array $data using the example below:

    public function submit($request)
    {
        $data = $request->postVars();
    }

It seems that you can't get that Form $form from the arguments here.