Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Forms in a Custom Controller

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

Page: 1 2
Go to End
Author Topic: 2616 Views
  • tbarho
    Avatar
    Community Member
    41 Posts

    Forms in a Custom Controller Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Forms in a Custom Controller Link to this post

    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.

  • tbarho
    Avatar
    Community Member
    41 Posts

    Re: Forms in a Custom Controller Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Forms in a Custom Controller Link to this post

    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
    }

  • tbarho
    Avatar
    Community Member
    41 Posts

    Re: Forms in a Custom Controller Link to this post

    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;

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Forms in a Custom Controller Link to this post

    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.

  • tbarho
    Avatar
    Community Member
    41 Posts

    Re: Forms in a Custom Controller Link to this post

    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.

  • tbarho
    Avatar
    Community Member
    41 Posts

    Re: Forms in a Custom Controller Link to this post

    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. . .

    2616 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.