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

Forms in a Custom Controller


Go to End


10 Posts   5813 Views

Avatar
tbarho

Community Member, 41 Posts

8 January 2010 at 6:56am

Hello all!

I'm trying to get a Form into a custom controller. I've used the post at http://doc.silverstripe.org/doku.php?id=controller to play around with the custom controller, and now I'm trying to get a form into that page, but it just shows up blank, and I'm not understanding why. Here's a sample of the code I'm using in FastFood.php

class FastFood_Controller extends Controller {
    function order($arguments) {
        print_r($arguments);
    }
    
    function newOrder() {
    	
    	$fields = singleton('SomeObject')->getFrontendFields();
		
		$actions = new FieldSet(
			new FormAction('someAction', 'Submit')
		);
		
		
		$form = new Form(
			$this,
			'Form',
			$fields,
			$actions
		);
		
		return $form;
    
    }
}

Any ideas where I'm going wrong? Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

8 January 2010 at 1:19pm

The second parameter to the Form object should be the name of the current form function. Currently you have 'Form' whereas your function is called newOrder.

Avatar
tbarho

Community Member, 41 Posts

9 January 2010 at 7:50am

Ok, changed the code to

class FastFood_Controller extends Controller {
    function order($arguments) {
        print_r($arguments);
    }
    
    function newOrder() {
    	
    	$fields = singleton('Reference')->getFrontendFields();
		
		$actions = new FieldSet(
			new FormAction('someAction', 'Submit')
		);
		
		
		$form = new Form(
			$this,
			'newOrder',
			$fields,
			$actions
		);
		
		return $form->__construct($this, "MyForm", $fields, $actions);
    
    }
}

and still getting a blank page. Any thoughts?

Avatar
Willr

Forum Moderator, 5523 Posts

9 January 2010 at 10:19am

Edited: 09/01/2010 10:20am

What are you trying to do with this code

  $form = new Form( 
         $this, 
         'newOrder', 
         $fields, 
         $actions 
      ); 
       
      return $form->__construct($this, "MyForm", $fields, $actions);

The constructor is called when you create the new Form. You should be just returning the $form you created above that line like return $form;

Also note you have a form action called 'someAction' but that code you posted doesn't have a someAction(). You will need to create a function (if you haven't already) like

function someAction($data, $form) {
// do stuff
}

Avatar
tbarho

Community Member, 41 Posts

9 January 2010 at 10:41am

Edited: 09/01/2010 10:41am

Yeah, I'm basically just trying to get the controller to spit out a form on the page. I don't care if it does anything yet, I just want to get the form displayed, which I can't seem to do. All I can get so far is a blank page, even if I try

 return $form; 

Avatar
Willr

Forum Moderator, 5523 Posts

9 January 2010 at 10:47am

Have you got display_errors turned on in your php.ini file. Or check the server logs for an error. Could be a typo with the code. If you want to see where in the code its breaking you could try inserting a die('Hi'); statement at the top of the form function and check that it appears, then move it down till you have it disappear.

Avatar
tbarho

Community Member, 41 Posts

13 January 2010 at 10:32am

Edited: 13/01/2010 10:34am

So did the die("hi") thing, and it made it through the entire newOrder() method. Here's the full code I have now at FastFood.php:

class FastFood_Controller extends Controller {
    function order($arguments) {
        print_r($arguments);
    }
    
    function newOrder() {
    	
    	$fields = singleton('Reference')->getFrontendFields();
		
		
    	
    	
		$actions = new FieldSet(
			new FormAction('someAction', 'Submit')
		);
		
		
		
		$form = new Form(
			$this,
			'newOrder',
			$fields,
			$actions
		);
		
		
		
		return $form;
    
    }
    
    function someAction($data, $form) {
    
    	die("hi");
    }
}

And also my _config.php changes:

Director::addRules(50, array('fastfood/$Action/$ID/$Name' => 'FastFood_Controller'));

All I see on the page is "hi", which is weird, because that someAction() method shouldn't be called yet, right? Could it be that since I'm not inheriting from Page_Controller, and just controller, there's nothing to render the form object that's returned? I'm basically just trying to experiment with building a more standard MVC application outside of the CMS framework, so I'm trying to avoid the inheritance of CMS objects.

Avatar
tbarho

Community Member, 41 Posts

13 January 2010 at 11:14am

Yeah, it looks like the newOrder() method is returning the form object, but there's no way to render it. Not sure how to get past that. . .

Go to Top