Jump to:

1970 Posts in 1272 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » problem with form templating [SOLVED]

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

Page: 1
Go to End
Author Topic: 1628 Views
  • Entar
    Avatar
    Community Member
    18 Posts

    problem with form templating [SOLVED] Link to this post

    Hi all,

    My problem is in form customizing, here's what I'm trying to do:

    code/MyPage.php

    class MyPage extends Page {
    ...
    }

    class MyPage_Controller extends Page_Controller {
       function init() {
          parent::init();
          Requirements::javascript("mysite/javascript/jquery-1.2.6.min.js");
          Requirements::javascript("mysite/javascript/order.js");
       }

       function DoOrder() {
          $fields = new FieldSet(
             new TextField(
                $name = "name",
                $title = "title",
                $value = "asd"
             )
          );
          $actions = new FieldSet(new FormAction("send", "Send"));
          $required = new RequiredFields("name");
          $form = new Form($this, "DoOrder", $fields, $actions, $required);

          //here I tried two ways to achieve the form being skinned, but all of them gave me same error (will be listed below)
          //first way
          // $form = $this->customise($form)->renderWith('MyTemplate');
          // return $form;
          //second way
          // $form = $form->renderWith('MyTemplate');
          // return $form;
          //well, last way - no template at all and only in this case form and form submission is working
          return $form;
       }

       function send($data, $form ) {
          print_r($form);
       }
    }

    templates/MyPage.ss

       $Content
       $DoOrder

    templates/includes/MyTemplate.ss

       // here is my template for form, meaningless to write it here

    Well, it's look all good, and it's even work and actually apply a template to form in first two cases (commented in code) and then render it to browser, but upno form submission an error acquires:

    Fatal error: Call to a member function loadDataFrom() on a non-object in C:\Server\httpd\web\sapphire\core\control\Controller.php on line 178

    Is there any other ways to customize form without errors? Or maybe my way to do this is good but not complete?

  • Entar
    Avatar
    Community Member
    18 Posts

    Re: problem with form templating [SOLVED] Link to this post

    Forgot to say - I need to skin exactly a form object, not page object, so templating directly in MyPage.ss isn't good for me, because for example "<% control Fields %>" will be unavailable in this case.

  • Entar
    Avatar
    Community Member
    18 Posts

    Re: problem with form templating [SOLVED] Link to this post

    [SOLVED]

    I found solution, maybe it's not very good, but it's working:

    code/MyPage.php

    class MyPage extends Page {
       ...
    }

    class MyPage_Controller extends Page_Controller {
       function init() {
          parent::init();
          Requirements::javascript("mysite/javascript/jquery-1.2.6.min.js");
          Requirements::javascript("mysite/javascript/order.js");
       }

       function DoOrder() {
          $fields = new FieldSet(
             new TextField(
                $name = "name",
                $title = "title",
                $value = "asd"
             )
          );
          $actions = new FieldSet(new FormAction("send", "Send"));
          $required = new RequiredFields("name");
          $form = new MyForm($this, "DoOrder", $fields, $actions, $required);
          return $form;
       }

       function send($data, $form ) {
          print_r($form);
       }
    }

    class MyForm extends Form {
       function forTemplate() {
          return $this->renderWith("MyForm");
       }
    }

    And two templates, first that just initialize DoOrder, and second that skin a form. Thats it. Working great, without any errors. Maybe it'll be useful for somebody, who faced this problem too...

    1628 Views
Page: 1
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.