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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Themes /

Discuss SilverStripe Themes.

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

Losing connection to controller with renderWith()


Go to End


3 Posts   1735 Views

Avatar
sonet

Community Member, 33 Posts

28 June 2010 at 7:59am

Edited: 28/06/2010 8:32am

I want to add simple, custom control (newsletter signup form, which is being rendered with a custom form template). In code below, when returning $formSignup only, everything works fine. When using the renderWith() method the proper action (/home/SignupForm in URL) is being called, but somehow the connection to the page controller is lost. I am not sure what I am doing wrong. The custom form is being send to the browser only completely detached.

	/**
	* This function puts a form on your page, using $Form.
	*/
	public function SignupForm() {
		$formSignup = new Form($this, "SignupForm", new FieldSet(
				new EmailField("Email", "","E-Mail Address")),
				new FieldSet(new FormAction("SignupAction", "Sign up"))//,
				//new RequiredFields("Email")
			);
		//return $formSignup;
		return $this->customise($formSignup)->renderWith(array('FormSignup'));
	}

	/**
	* This function is called when the user submits the form.
	*/
	public function SignupAction($data, $form) {

		// Create a new Member object and load the form data into it
		$member = new Member();
		$form->saveInto($member);

		// Write it to the database. This needs to happen before we add it to a group
		$member->write();

		// Add the member to group. (Check if it exists first)
		if($group = DataObject::get_one('Group', "ID = $this->defaultGroupID")) { 

			$member->Groups()->add($group);
			// Redirect to a page thanking people for registering
			Director::redirect('signup-thank-you/');
			
		} else {
			// Redirect to a failure page
			Director::redirect('signup-failure/');
		}
	}

I appreciate any help.

Avatar
sonet

Community Member, 33 Posts

1 July 2010 at 4:21am

Avatar
CHD

Community Member, 219 Posts

11 June 2011 at 4:08am

this may come in handy for anybody who wants to easily include forms in a sidebar or any other area of the site, without controlling a hidden page:

http://www.clickheredigital.co.uk/blog/how-to-include-a-silverstripe-form-on-any-every-page/

it also has an easy "query database for existing member" function that isn't covered in the form tutorials...