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:

Widgets /

Discuss SilverStripe Widgets.

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

Form Widget doesn't work on Blog Pages


Go to End


3 Posts   2232 Views

Avatar
socks

Community Member, 191 Posts

2 July 2011 at 2:57pm

Edited: 02/07/2011 2:57pm

I made some custom widgets. All is well except that my Form Widget doesn't work on Blog pages (it does work on all other pages).

Error:
popCurrent called on ModelAsController controller, but it wasn't at the top of the stack
POST /websitename/blog/widget/24/FormWidget

Widget Form Code

class FormWidget_Controller extends Widget_Controller {

	function FormWidget() {
		
		return new Form(
			$this, 
			'FormWidget', 
			new FieldSet(
				new EmailField('EmailFormWidget')
			), 
			new FieldSet(
				new FormAction('doFormWidget', 'Submit')
			),
			new RequiredFields('EmailFormWidget')
		);
		
	}
   
  function doFormWidget($data, $form) {

		//Set data
		$from = $data['EmailFormWidget'];
		$to = 'example@example.com';
		$subject = "this is the subject";   
		$body = '<h1>Email Signup List</h1><p><b>Email Address</b> - '.$data['EmailFormWidget'].'</p>';  
		$email = new Email($from, $to, $subject, $body);
		$email->send();
		Director::redirect(Director::get_current_page()->Link() . "?success=1");
	}
	
	// Allows us to show different content in template "if Success"
	public function SuccessFormWidget() {
		return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
	}
}

Thank you

Avatar
Matze0681

Community Member, 25 Posts

10 July 2012 at 8:46pm

Edited: 10/07/2012 8:46pm

I have the same issue. I know it is an old post, but did you find a solution for this?

Thanks in advance
Matze

Avatar
Bambii7

Community Member, 254 Posts

24 July 2013 at 5:16pm

I know this is old but recently built a blog search widget in SS 2.4, which turned into a real nightmare. So if anyone else has to work with an old ss install heres what I did. With some search specific code removed, it was ripped & modified code from MySQLDatabase->searchEngine()

  • Create a custom widget which returns a from from a custom controller
  • Create custom controller which has a form function and a results view or action
  • Create a form with its action set to the custom controller
  • Pretty print, pipe the nice url to the custom controller to avoid controller names appearing in the url

class BlogSearchWidget extends Widget {
	
	static $cmsTitle = "Blog Search";
	static $description = "Limits search results to Blog pages only.";
	
	public function Title() {
		return "Search Blog";
	}
	
	public function SearchForm() {
                // return the from from a custom controller
		$formController = new BlogSearch_Controller();
		return $formController->search();
	}

}

class BlogSearch_Controller extends ContentController {

	static $allowed_actions = array(
		'search',
	);

	public function index() {
                 // render search results
        }
	
	function search() {
		return new BlogSearchForm($this, 'blogsearch');
	}

}

class BlogSearchForm extends Form {
 
   function __construct($controller, $name) {
	   
		$fields = new FieldSet(
			new TextField('query', 'Search')
		);

		$actions = new FieldSet(
			new FormAction('submit', '>> Start Search ')
		);
		
		$this->setFormMethod('get');
		$this->disableSecurityToken();
		$this->setFormAction('blogsearch');
		parent::__construct($controller, $name, $fields, $actions);
		
	}
     
   function forTemplate() {
		return $this->renderWith(array(
					$this->class,
					'Form'
		));
	}
	
}

Director::addRules(50, array(
	'blogsearch' => 'BlogSearch_Controller'
	)
);