PHP code example of silverstripe / multiform

1. Go to this page and download the library: Download silverstripe/multiform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silverstripe / multiform example snippets


use SilverStripe\MultiForm\Forms\MultiForm;

class SurveyForm extends MultiForm
{

}

use SilverStripe\MultiForm\Models\MultiFormStep;

class PersonalDetailsStep extends MultiFormStep
{

}

use SilverStripe\MultiForm\Forms\MultiForm;

class SurveyForm extends MultiForm
{
    private static $start_step = PersonalDetailsStep::class;
}

use SilverStripe\MultiForm\Models\MultiFormStep;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;

class PersonalDetailsStep extends MultiFormStep
{
    private static $next_steps = OrganisationDetailsStep::class;

    public function getFields()
    {
        return FieldList::create(
            TextField::create('FirstName', 'First name'),
            TextField::create('Surname', 'Surname')
        );
    }
}

use SilverStripe\MultiForm\Models\MultiFormStep;

class OrganisationDetailsStep extends MultiFormStep
{
    private static $is_final_step = true;

    ...
}

use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\ORM\FieldType\DBHTMLText;

class PageController extends ContentController
{
    private static $allowed_actions = [
        'SurveyForm',
        'finished'
    ];

    public function SurveyForm()
    {
        return SurveyForm::create($this, 'SurveyForm');
    }

    public function finished()
    {
        return [
            'Title' => 'Thank you for your submission',
            'Content' => DBHTMLText::create('<p>You have successfully submitted the form!</p>')
        ];
    }
}

use SilverStripe\MultiForm\Models\MultiFormStep;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;

class Step1 extends MultiFormStep
{
    private static $next_steps = Step2::class;

    public function getFields()
    {
        return FieldList::create(
            EmailField::create('Email', 'Your email')
        );
    }
}

use SilverStripe\MultiForm\Models\MultiFormStep;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;

class Step2 extends MultiFormStep
{
    private static $next_steps = Step3::class;

    public function getFields()
    {
        $fields = FieldList::create(
            EmailField::create('Email', 'E-mail'),
            EmailField::create('Email2', 'Verify E-Mail')
        );

        // set the email field to the input from Step 1
        $this->copyValueFromOtherStep($fields, 'Step1', 'Email');

        return $fields;
    }
}

use SilverStripe\MultiForm\Forms\MultiForm;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;

class SurveyForm extends MultiForm
{
   private static $start_step = PersonalDetailsStep::class;

   public function finish($data, $form)
   {
      parent::finish($data, $form);

      $steps = MultiFormStep::get()->filter([
        "SessionID" => $this->session->ID
      ]);

      if ($steps) {
         foreach ($steps as $step) {
            if ($step->ClassName == PersonalDetailsStep::class) {
               $member = Member::create();
               $data = $step->loadData();

               if ($data) {
                  $member->update($data);
                  $member->write();
               }
            }

            if ($step->ClassName == OrganisationDetailsStep::class) {
               $organisation = Organisation::create();
               $data = $step->loadData();

               if ($data) {
                  $organisation->update($data);

                  if ($member && $member->ID) {
                    $organisation->MemberID = $member->ID;
                  }

                  $organisation->write();
               }
            }
            // Shows the step data (unserialized by loadData)
            // Debug::show($step->loadData());
         }
      }

      $this->controller->redirect($this->controller->Link() . 'finished');
   }
}

use  SilverStripe\ORM\DataObject;

class Organisation extends DataObject
{
    private static $db = [
        // Add your Organisation fields here
    ];
}

$this->session->delete();

class MyStep extends MultiFormStep
{
   ...

   public function getNextStep()
   {
      $data = $this->loadData();
      if(isset($data['Gender']) && $data['Gender'] == 'Male') {
         return TestThirdCase1Step::class;
      } else {
         return TestThirdCase2Step::class;
      }
   }

   ...
}

class MyStep extends MultiFormStep
{
   ...

   public function getValidator()
   {
      return RequiredFields::create(array(
         'Name',
         'Email'
      ));
   }

   ...
}

use SilverStripe\Dev\Debug;
use SilverStripe\MultiForm\Forms\MultiForm;
use SilverStripe\MultiForm\Models\MultiFormStep;

class SurveyForm extends MultiForm
{
   private static $start_step = PersonalDetailsStep::class;

   public function finish($data, $form)
   {
      parent::finish($data, $form);

    $steps = MultiFormStep::get()->filter(['SessionID' => $this->session->ID]);

      if($steps) {
         foreach ($steps as $step) {
            // Shows the step data (unserialized by loadData)
            Debug::show($step->loadData());
         }
      }
   }
}

$this->session->delete();