Reputation: 275
I have the following form code:
ContactPage_Controller extends Page_Controller {
static $allowed_actions = array(
'submit'
);
public function Send() {
$contact = new Contact();
$validator = new RequiredFields('Anrede', 'Vorname', 'Nachname', 'Email', 'Message');
$fields = new FieldList(
new TextField('securityidentifier', 'securityidentifier', $this->cryptTime()),
new DropdownField('Anrede', 'Anrede:*', $contact->dbObject('Anrede')->enumValues()),
new TextField('Vorname', 'Vorname:*'),
new TextField('Nachname', 'Nachname:*'),
new EmailField('Email', 'E-Mail-Adresse:*'),
new TextareaField('Message', 'Nachricht:*')
);
$actions = new FieldList(
new FormAction('submit', 'Senden')
);
return new Form($this, 'submit', $fields, $actions, $validator);
}
// ...
}
This works fine if I use it as a single Page. When I try to use it as child page the form does not show up. I get the headline, I get the content but I don't get the form.
Any ideas why this isn't working correctly?
Upvotes: 0
Views: 68
Reputation: 1584
Your form is defined on ContactPage_Controller
. If the child page on which you're trying to render it is not a descendant of ContactPage
, then that method is not visible through inheritance. There are several options.
1) Put the form in Page_Controller
. Typically, all your pages inherit from that, so they will have access to the form. Not great practice, though. Try to keep your Page
classes lean.
2) Create an extension that injects the form factory method and handler into a controller. This is more of the "horizontal" approach rather than the vertical approach of inheritance, and tends to scale better.
3) Along the same lines, create a trait that does the same thing. Probably a better option, since extensions are idiosyncratic and will probably be deprecated at some point.
Upvotes: 4