Here is a basic example form with one required text field and one submit button, which submits to a custom function:
class Page_Controller extends ContentController {
private static $allowed_actions = array(
'ExampleForm'
);
public function ExampleForm() {
$fields = FieldList::create(
TextField::create('Name', 'Your Name')
);
$actions = FieldList::create(
FormAction::create('doExampleFormAction', 'Go')
);
$requiredFields = RequiredFields::create('Name');
$form = Form::create(
$this,
'ExampleForm',
$fields,
$actions,
$requiredFields
);
return $form;
}
public function doExampleFormAction($data, Form $form) {
$form->sessionMessage('Hello '. $data['Name'], 'success');
return $this->redirectBack();
}
}
To display this form we add $ExampleForm
to our page template:
$ExampleForm