1794 Posts in 590 Topics by 562 members
| Go to End | Next > | |
| Author | Topic: | 2008 Views |
-
Forms in a Custom Controller

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!
-
Re: Forms in a Custom Controller

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.
-
Re: Forms in a Custom Controller

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?
-
Re: Forms in a Custom Controller

9 January 2010 at 10:19am Last edited: 9 January 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
} -
Re: Forms in a Custom Controller

9 January 2010 at 10:41am Last edited: 9 January 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;
-
Re: Forms in a Custom Controller

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.
-
Re: Forms in a Custom Controller

13 January 2010 at 10:32am Last edited: 13 January 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. -
Re: Forms in a Custom Controller

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. . .
| 2008 Views | ||
| Go to Top | Next > |


