2
votes

If I click the submit button to send data from my SilverStripe form to the database (xampp, mysql), it doesn't redirect me back to the form page. Instead it redirects to {BaseUrl}name_of_controller/name_of_form -> in my case: localhost/pmtool/DeveloperController/NewTaskMask. I have searched for hours for the solution of this problem, but without any success.

Here's the code of the file, in which the form and the action is defined. In another html-file the form will be called through $NewTaskMask. By the URL localhost/pmtool/developer/newtask the function renderNewTaskForm will be called, the template with $NewTaskMask will be rendered.

All fields such as Title, Description, etc do exist in the database.

class DeveloperController extends ContentController {

    private static $allowed_actions = array('NewTaskMask','renderNewTaskForm');

    private static $url_handlers = array(
        'newtask' => 'renderNewTaskForm'
    );

    public function renderNewTaskForm(SS_HTTPRequest $request) {
        return $this->renderWith('NewTaskForm');
    }

    public function NewTaskMask() {
        $fields = new FieldList(
            TextField::create('Title','Title')
                ->setAttribute('autocomplete', 'off')
                ->setAttribute('placeholder', 'Enter a task title ...'),
            TextareaField::create('Description','Description')
                ->setAttribute('autocomplete', 'off'),
            DropdownField::create('Project','Project', Project::get()->map('ID','Title')),
            DropdownField::create('Developer','Developer', Developer::get()->map('ID','Title'))
        );

        $actions = new FieldList(FormAction::create('doCreateT','Task anlegen'));

        $requiredFields = new RequiredFields(array('Title'));

        return new Form($this, 'NewTaskMask', $fields, $actions, $requiredFields);
    }

    public function doCreateT($data, $form) {

        // Creating a new task record
        $nT = new Task();

        $nT->Title = $data['Title'];
        $nT->Description = $data['Description'];
        $nT->Project = $data['ProjectID'];
        $nT->Developer = $data['DeveloperID'];
        $nT->write();

        // Create a nice msg for our users
        $form->sessionMessage('Task angelegt!','good');

        // Redirect back to the form page
        return $this->redirectBack();
    }
}
2

2 Answers

1
votes

Seems like an issue with routing. I assume you've set up the developer route to hit DveloperController. Doing the same I could reproduce your error.

I tried explicitly adding another route for DeveloperController to hit the DeveloperController controller, because it wasn't redirecting properly. It's a pretty weird and magic fix and to be honest I'm not sure why it works, but it solved it:

---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'developer//$Action/$ID/$Name': 'DeveloperController'
    'DeveloperController//$Action/$ID/$Name': 'DeveloperController'

Update the routes to correspond with your setup and don't forget to run ?flush afterwards.

Maybe someone with more insight can explain what's going on.

0
votes

I want to render the page "localhost/pmtool/developer/$ID" with a template. So I added '$ID!' => 'index' to the url_handlers. But now it's the same problem as in the beginning. Do I have to add another route for this problem or has the line '$ID!' => 'index' a bug?

private static $url_handlers = array(
    'newtask' => 'renderNewTaskForm',
    '$ID!' => 'index'
);

public function index(SS_HTTPRequest $request) {
        return $this->renderWith('DeveloperTemplate');
}