Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Creating a new custom form (Not using the CMS Controllers)


Go to End


2 Posts   1282 Views

Avatar
Naveed

Community Member, 10 Posts

10 May 2016 at 10:49am

Edited: 10/05/2016 11:49am

Hi Experts,

I am trying to create a new custom form that is extended from the "Controller" class. I have seen many tutorials but all the controllers are extending from CMS Controllers. I am trying to use the case when the CMS bundle/package is not included and building it from scratch. Here is my code i.e.

Controller location: mysite/code/controllers/GalleryController.php

 
class GalleryController extends Controller {
    private static $allowed_actions = array(
        'index',
        'addForm' => 'ADMIN',
    );

    public function index(SS_HTTPRequest $request)
    {
        return $this->getResponse();
    }
    
    public function add(SS_HTTPRequest $request){
        $fields = new FieldList(
            TextField::create('imageName', 'Image Name')
        );
        
        $actions = new FieldList(
            FormAction::create("galleryActMsgs")->setTitle("Gallery Images Area")
        );
        
        $form = new Form($this, 'addForm', $fields, $actions, $required);
        
        return $form;
    }
    
    public function galleryActMsgs($data, Form $form){
        $form ->sessionMessage("Data Saved Successfully....", 'success');
        
        return $this->redirectBack();
    }

Template file created at: mysite/templates/GalleryController_add.ss

 $addForm

URL to access the above controller i.e.

http://localhost:8011/ss_pac/gallery/addForm
NOTE: The above URL was working fine before adding form code.
 

Error by accessing the above url:
There seems to have been a technical problem. Please click the back button, refresh your browser, and try again.

I tried the following steps i.e.
http://localhost:8011/ss_pac/dev/build?flush=all The above url build everything without any error.
http://localhost:8011/ss_pac/gallery/addForm?flush=1 This gives the same above error.

Looking for a quick help on it and hope this will be helpful for others as well.

Cheers,
Naveed.

Avatar
ZarockNZ

Community Member, 17 Posts

1 August 2016 at 9:26pm

Hi,

I'm no expert, but I think I spot a few problems here.

The biggest issue I see is that the allowed action of 'addForm' does not match the name of the function which is just 'add'. Please try calling the function addForm to match the rest of the code.

Also I am wondering, if you are not using the CMS package can someone be logged in as ADMIN? For the allowed actions have you tried changing 'addForm' => 'ADMIN', to just 'addForm'?

Also where the form is defined $form = new Form($this, 'addForm', $fields, $actions, $required); the variable $required is not created/specified anywhere beforehand.

Hope this helps.