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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to make View for custom SilverStripe Controller in correct way?


Go to End


2 Posts   1076 Views

Avatar
Bogoed Shamansky

Community Member, 12 Posts

20 October 2014 at 11:32am

Edited: 20/10/2014 9:41pm

What is the correct way to View my Controller's Form and other Actions without creation the new Page Type? Now i created a new Page Type and show in it's layout the form to add some information. On this form I need to add captcha only for guests users in front-end and show the same form (and other actions returned values) for custom user groups in back-end but without captcha.

Class for Database model (mysite/code/mineDataObject.php):

<?php 
class mineDataObject extends DataObject{	
	private static $db = array(
		'Name' => 'Varchar(256)',
		'Street' => 'Varchar(56)',
		'Telephone' => 'Varchar(11)'
	);
}
?>

Controller class (mysite/code/mineController.php):

<?php
class mine extends Page{}

class mine_Controller extends Page_Controller{
	private static $allowed_actions = array('mineForm');
	public function mineForm(){
		$fields = new FieldList(
			new TextField('Name', 'Name'),
			new TextField('Street', 'Street'),
			new PhoneNumberField('Telephone', 'Telephone')
		);
		$actions = new FieldList(
			new FormAction('doAdd','Submit')
		);
		$validator = new RequiredFields('Name', 'Street', 'Telephone'); 
		return new Form($this, 'mineForm', $fields, $actions, $validator);
	}
	
	public function doAdd($data, $form) {
		$submission = new mineDataObject();
		$form->saveInto($submission);
		$submission->write();
		return $this->redirectBack();
	}
}
?>

If it's already right way - then how to make that users will can create only one instance of this Page Type?

I tried to config Director for my controllers form action, but it's not work for me.

routes.yml
---
Name: mineroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'mine//$Action' : 'mine_Controller'

If it's right way then I just need to add links for controller and it's actions in Page. ss template, but how to configure permissions to it, for example hide some action from site guest and show it only to group named "Site Redactor"?
If go to http://myweb.site/mine/mineForm - it will return error.
Thanks a lot for any help!

Avatar
Nivanka

Community Member, 400 Posts

20 October 2014 at 1:53pm

I suggest you to use a mother action like displayform and have a template file like MyController_displayform.ss and invoke
The from from the template file.