API Strongly-type action method signatures

This commit is contained in:
Steve Boyd 2022-10-19 12:00:04 +13:00
parent 35dcfab57f
commit f18cbb00a5
7 changed files with 23 additions and 15 deletions

View File

@ -91,7 +91,7 @@ class FrameworktestRegressSessionAdmin extends Controller
return $form; return $form;
} }
public function startsession($data, $form) public function startsession(array $data, Form $form): HTTPResponse
{ {
Session::set('enabletranslatable', (isset($data['enabletranslatable'])) ? $data['enabletranslatable'] : null); Session::set('enabletranslatable', (isset($data['enabletranslatable'])) ? $data['enabletranslatable'] : null);
Session::set('db', $data['db']); Session::set('db', $data['db']);

View File

@ -6,6 +6,7 @@ use SilverStripe\Security\Member;
use SilverStripe\Forms\EmailField; use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\FileField; use SilverStripe\Forms\FileField;
use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction; use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
@ -38,11 +39,11 @@ class TestFileUploadPage_Controller extends TestPage_Controller
return new Form($this, "Form", $fields, $actions); return new Form($this, "Form", $fields, $actions);
} }
public function addMember($data, $form) public function addMember(array $data, Form $form): HTTPResponse
{ {
$member = new Member(); $member = new Member();
$form->saveInto($member); $form->saveInto($member);
$member->write(); $member->write();
$this->redirectBack(); return $this->redirectBack();
} }
} }

View File

@ -13,6 +13,7 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\Control\Email\Email; use SilverStripe\Control\Email\Email;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Security\DefaultAdminService; use SilverStripe\Security\DefaultAdminService;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
@ -111,11 +112,11 @@ class TestPage_Controller extends PageController
return $form; return $form;
} }
public function save($data, $form) public function save(array $data, Form $form): HTTPResponse
{ {
$form->saveInto($this->dataRecord); $form->saveInto($this->dataRecord);
$this->dataRecord->write(); $this->dataRecord->write();
$this->redirectBack(); return $this->redirectBack();
} }
public function gohome() public function gohome()
@ -140,7 +141,8 @@ class TestPage_Controller extends PageController
); );
} }
public function sendEmail($data, $form) public function sendEmail(array $data, Form $form): HTTPResponse
{ {
$email = new Email(); $email = new Email();
$email->setTo($data['Email']); $email->setTo($data['Email']);
@ -149,6 +151,6 @@ class TestPage_Controller extends PageController
$email->setBody('A body with some umlauts: öäüß'); $email->setBody('A body with some umlauts: öäüß');
$email->send(); $email->send();
echo "<p>email sent to " . $data['Email'] . "</p>"; return HTTPResponse::create()->setBody("<p>email sent to " . $data['Email'] . "</p>");
} }
} }

View File

@ -1,17 +1,18 @@
<?php <?php
use SilverStripe\Control\HTTPResponse;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
class Page2MultiForm extends MultiForm class Page2MultiForm extends MultiForm
{ {
public static $start_step = 'Page2PersonalDetailsFormStep'; public static $start_step = 'Page2PersonalDetailsFormStep';
public function finish($data, $form) public function finish(array $data, Form $form): HTTPResponse
{ {
parent::finish($data, $form); parent::finish($data, $form);
$steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}"); $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");
@ -42,7 +43,7 @@ class Page2MultiForm extends MultiForm
} }
} }
$controller = $this->getController(); $controller = $this->getController();
$controller->redirect($controller->Link() . 'finished'); return $controller->redirect($controller->Link() . 'finished');
} }
} }

View File

@ -11,7 +11,7 @@ class Page3MultiForm extends MultiForm
{ {
public static $start_step = 'Page3StartFormStep'; public static $start_step = 'Page3StartFormStep';
public function finish($data, $form) public function finish(array $data, Form $form): HTTPResponse
{ {
parent::finish($data, $form); parent::finish($data, $form);
$steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}"); $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");
@ -42,7 +42,7 @@ class Page3MultiForm extends MultiForm
} }
} }
$controller = $this->getController(); $controller = $this->getController();
$controller->redirect($controller->Link() . 'finished'); return $controller->redirect($controller->Link() . 'finished');
} }
} }

View File

@ -4,12 +4,14 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\LiteralField;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
class TestMultiForm extends MultiForm class TestMultiForm extends MultiForm
{ {
public static $start_step = 'TestMultiFormStepOne'; public static $start_step = 'TestMultiFormStepOne';
public function finish($data, $form) public function finish(array $data, Form $form): HTTPResponse
{ {
parent::finish($data, $form); parent::finish($data, $form);
@ -29,7 +31,7 @@ class TestMultiForm extends MultiForm
Session::set("MultiFormMessage", "Your information has been submitted."); Session::set("MultiFormMessage", "Your information has been submitted.");
$this->Controller()->redirect(Director::BaseURL() . $this->Controller()->URLSegment); return $this->Controller()->redirect(Director::BaseURL() . $this->Controller()->URLSegment);
} }
} }

View File

@ -7,6 +7,8 @@ use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPResponse;
class RecaptchaTestPage extends Page class RecaptchaTestPage extends Page
{ {
} }
@ -38,7 +40,7 @@ class RecaptchaTestPage_Controller extends PageController
return $form; return $form;
} }
public function submit($data, $form) public function submit(array $data, Form $form): HTTPResponse
{ {
$form->sessionMessage('Hooray!', 'good'); $form->sessionMessage('Hooray!', 'good');