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

A lot of forms so split some code


Go to End


3 Posts   1555 Views

Avatar
SilverPeed

Community Member, 22 Posts

27 November 2015 at 9:34pm

Hello,

I love the way silverstripe is building forms, it saves a lot of time. I'm building a CRM and all code is in one file called page.php. To gain some oversight i thought it would be a good idea to make a class where i can put, for example, all candidate forms. Below is my setup and i think i'm pretty close but its not working yet. If someone has some ideas on how i can make this setup work or how i can accomplish my goal in another way, it would be really nice.

--------------page.php----------------
private static $allowed_actions = array(
'LoadFormProjectkaart',
'ProjectkaartForm',
);
public function LoadFormProjectkaart(){
return $this->renderWith(array('ProjectkaartForm', 'Page'));
}
public function ProjectkaartForm(){
$test = new KandidaatForms();
$form = $test->ProjectkaartForm();
return $form;
}
--------------KandidaatForms.php-------------------
Class KandidaatForms{
public function ProjectkaartForm(){
$fields = new FieldList(
new TextField('Name'),
);
$validator = new RequiredFields();
$actions = new FieldList(
new FormAction('WriteProjectKaart', 'Aanmaken')
);
$form = new Form($this, 'ProjectkaartForm', $fields, $actions, $validator);
return $form;
}
}
-------------------ProjectKaartForm.ss-----------------
$ProjectkaartForm

With this code a receive the following error:
Call to undefined method KandidaatForms::hasMethod() in /Applications/XAMPP/xamppfiles/htdocs/recruitment/framework/forms/Form.php on line 908

Thanks in advance for any suggestions

Avatar
Devlin

Community Member, 344 Posts

2 December 2015 at 4:04am

The Form is expecting $this to be a controller -- "new Form($this)" -- for building an appropiate formaction url. It's asking the controller if it has a method. But your object has only one method.

To make your thing to work, you need to pass $this (the controller) to your ProjectkaartForm method in your KandidaatForms class.

private static $allowed_actions = array(
	'ProjectkaartForm',
);

public function ProjectkaartForm(){
	$test = new KandidaatForms();
	$form = $test->ProjectkaartForm($this);
	return $form;
}

class KandidaatForms {
	public function ProjectkaartForm($controller) {
		// ...
		$form = new Form($controller, 'ProjectkaartForm', $fields, $actions, $validator);
		return $form;
	}
}

Avatar
SilverPeed

Community Member, 22 Posts

3 December 2015 at 5:04am

Thanks Devlin, for your clear explanation. it works now :)