DOC added static::class calls and fixed some typos

This commit is contained in:
Franco Springveldt 2017-09-11 15:24:37 +12:00
parent 972786715c
commit 49610f2ce4

149
README.md
View File

@ -94,6 +94,8 @@ First of all, we need to create a new subclass of *MultiForm*.
For the above example, our multi-form will be called *SurveyForm* For the above example, our multi-form will be called *SurveyForm*
```php ```php
use SilverStripe\MultiForm\Models\MultiForm;
class SurveyForm extends MultiForm { class SurveyForm extends MultiForm {
} }
@ -112,7 +114,10 @@ So, for example, if we were going to have a first step which collects the
personal details of the form user, then we might have this class: personal details of the form user, then we might have this class:
```php ```php
class SurveyFormPersonalDetailsStep extends MultiFormStep { use SilverStripe\MultiForm\Models\MultiFormStep;
class PersonalDetailsStep extends MultiFormStep
{
} }
``` ```
@ -122,10 +127,11 @@ subclass of MultiForm, SurveyForm, and tell it that SurveyFormPersonalDetailsSte
is the first step. is the first step.
```php ```php
class SurveyForm extends MultiForm { use SilverStripe\MultiForm\Models\MultiForm;
private static $start_step = 'SurveyFormPersonalDetailsStep';
class SurveyForm extends MultiForm
{
private static $start_step = PersonalDetailsStep::class;
} }
``` ```
@ -141,17 +147,21 @@ To let the step know what step is next in the process, we do the same as setting
the `$start_step` variable *SurveyForm*, but we call it `$next_steps`. the `$start_step` variable *SurveyForm*, but we call it `$next_steps`.
```php ```php
class SurveyFormPersonalDetailsStep extends MultiFormStep { use SilverStripe\MultiForm\Models\MultiFormStep;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
private static $next_steps = 'SurveyFormOrganisationDetailsStep'; class PersonalDetailsStep extends MultiFormStep
{
private static $next_steps = OrganisationDetailsStep::class;
public function getFields() { public function getFields()
{
return FieldList::create( return FieldList::create(
TextField::create('FirstName', 'First name'), TextField::create('FirstName', 'First name'),
TextField::create('Surname', 'Surname') TextField::create('Surname', 'Surname')
); );
} }
} }
``` ```
@ -167,17 +177,18 @@ So, if we assume that the last step in our process is
SurveyFormOrganisationDetailsStep, then we can do something like this: SurveyFormOrganisationDetailsStep, then we can do something like this:
```php ```php
class SurveyFormOrganisationDetailsStep extends MultiFormStep { use SilverStripe\MultiForm\Models\MultiFormStep;
class OrganisationDetailsStep extends MultiFormStep
{
private static $is_final_step = true; private static $is_final_step = true;
} }
``` ```
### 5. Run database integrity check ### 5. Run database integrity check
We need to run *dev/build?flush=1* now, so that the classes are available to the We need to run *dev/build?flush=1* now, so that the classes are available to the
SilverStripe manifest builder, and to ensure that the database is up to scratch SilverStripe manifest builder, and to ensure that the database is up to date
with all the latest tables. So you can go ahead and do that. with all the latest tables. So you can go ahead and do that.
*Note: Whenever you add a new step, you **MUST** run dev/build?flush=1 or you *Note: Whenever you add a new step, you **MUST** run dev/build?flush=1 or you
@ -190,35 +201,27 @@ So, if we want to render our multi-form as `$SurveyForm` in the *Page.ss*
template, we need to create a SurveyForm method (function) on the controller: template, we need to create a SurveyForm method (function) on the controller:
```php ```php
class Page extends SiteTree { use SilverStripe\CMS\Controllers\ContentController;
// ... class PageController extends ContentController
{
} private static $allowed_actions = [
class Page_Controller extends ContentController {
// ...
//
private static $allowed_actions = array(
'SurveyForm', 'SurveyForm',
'finished' 'finished'
); ];
public function SurveyForm() { public function SurveyForm()
return SurveyForm::create($this, 'Form'); {
return SurveyForm::create($this, 'SurveyForm');
} }
public function finished() { public function finished()
return array( {
return [
'Title' => 'Thank you for your submission', 'Title' => 'Thank you for your submission',
'Content' => '<p>You have successfully submitted the form!</p>' 'Content' => '<p>You have successfully submitted the form!</p>'
); ];
} }
// ...
} }
``` ```
@ -334,22 +337,34 @@ based on the submission value of another step. There are two methods supporting
Here is an example of how to populate the email address from step 1 in step2 : Here is an example of how to populate the email address from step 1 in step2 :
```php ```php
use SilverStripe\MultiForm\Models\MultiFormStep;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;
class Step1 extends MultiFormStep class Step1 extends MultiFormStep
{ {
private static $next_steps = 'Step2'; private static $next_steps = Step2::class;
public function getFields() { public function getFields()
{
return FieldList::create( return FieldList::create(
EmailField::create('Email', 'Your email') EmailField::create('Email', 'Your email')
); );
} }
} }
```
```php
use SilverStripe\MultiForm\Models\MultiFormStep;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;
class Step2 extends MultiFormStep class Step2 extends MultiFormStep
{ {
private static $next_steps = 'Step3'; private static $next_steps = Step3::class;
public function getFields() { public function getFields()
{
$fields = FieldList::create( $fields = FieldList::create(
EmailField::create('Email', 'E-mail'), EmailField::create('Email', 'E-mail'),
EmailField::create('Email2', 'Verify E-Mail') EmailField::create('Email2', 'Verify E-Mail')
@ -378,11 +393,16 @@ So, we must write some code on our subclass of *MultiForm*, overloading
Here is an example of what we could do here: Here is an example of what we could do here:
```php ```php
class SurveyForm extends MultiForm { use SilverStripe\MultiForm\Models\MultiForm;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;
class SurveyForm extends MultiForm
{
private static $start_step = PersonalDetailsStep::class;
private static $start_step = 'SurveyFormPersonalDetailsStep'; public function finish($data, $form)
{
public function finish($data, $form) {
parent::finish($data, $form); parent::finish($data, $form);
$steps = DataObject::get( $steps = DataObject::get(
@ -392,7 +412,7 @@ class SurveyForm extends MultiForm {
if($steps) { if($steps) {
foreach($steps as $step) { foreach($steps as $step) {
if($step->class == 'SurveyFormPersonalDetailsStep') { if($step->class == PersonalDetailsStep::class) {
$member = Member::create(); $member = Member::create();
$data = $step->loadData(); $data = $step->loadData();
@ -402,7 +422,7 @@ class SurveyForm extends MultiForm {
} }
} }
if($step->class == 'SurveyOrganisationDetailsStep') { if($step->class == OrganisationDetailsStep::class) {
$organisation = Organisation::create(); $organisation = Organisation::create();
$data = $step->loadData(); $data = $step->loadData();
@ -436,8 +456,10 @@ This example has been chosen as a separate DataObject but you may wish to change
the code and add the data to the Member class instead. the code and add the data to the Member class instead.
```php ```php
class Organisation extends DataObject { use SilverStripe\ORM\DataObject;
class Organisation extends DataObject
{
private static $db = array( private static $db = array(
// Add your Organisation fields here // Add your Organisation fields here
); );
@ -502,20 +524,20 @@ step should be. An example:
```php ```php
class MyStep extends MultiFormStep class MyStep extends MultiFormStep
{
...
// ... public function getNextStep()
{
public function getNextStep() {
$data = $this->loadData(); $data = $this->loadData();
if(@$data['Gender'] == 'Male') { if($data['Gender'] == 'Male') {
return 'TestThirdCase1Step'; return TestThirdCase1Step::class;
} else { } else {
return 'TestThirdCase2Step'; return TestThirdCase2Step::class;
} }
} }
// ... ...
} }
``` ```
### Validation ### Validation
@ -527,11 +549,12 @@ validation see [:form](http://doc.silverstripe.org/form-validation).
e.g. e.g.
```php ```php
class MyStep extends MultiFormStep { class MyStep extends MultiFormStep
{
... ...
public function getValidator() { public function getValidator()
{
return RequiredFields::create(array( return RequiredFields::create(array(
'Name', 'Name',
'Email' 'Email'
@ -539,7 +562,6 @@ class MyStep extends MultiFormStep {
} }
... ...
} }
``` ```
@ -557,16 +579,19 @@ won't be saved.
For example: For example:
```php ```php
class SurveyForm extends MultiForm { use SilverStripe\Dev\Debug;
use SilverStripe\MultiForm\Models\MultiForm;
use SilverStripe\MultiForm\Models\MultiFormStep;
private static $start_step = 'SurveyFormPersonalDetailsStep'; class SurveyForm extends MultiForm
{
private static $start_step = PersonalDetailsStep::class;
public function finish($data, $form) { public function finish($data, $form)
{
parent::finish($data, $form); parent::finish($data, $form);
$steps = MultiFormStep::get()->filter(array( $steps = MultiFormStep::get()->filter(['SessionID' => $this->session->ID]);
"SessionID" => $this->session->ID
));
if($steps) { if($steps) {
foreach($steps as $step) { foreach($steps as $step) {