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

problem with form templating [SOLVED]


Go to End


3 Posts   3046 Views

Avatar
Entar

Community Member, 23 Posts

28 December 2008 at 7:58am

Edited: 28/12/2008 12:12pm

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?

Avatar
Entar

Community Member, 23 Posts

28 December 2008 at 8:03am

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.

Avatar
Entar

Community Member, 23 Posts

28 December 2008 at 12:09pm

Edited: 28/12/2008 12:11pm

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