Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObjectManager Submit from Web Form

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1214 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    DataObjectManager Submit from Web Form Link to this post

    I have a custom web form form, how do I submit to a DataObjectManager table?

    Rick

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Submit from Web Form Link to this post

    You can just use $form->saveInto($dataobject). Just make sure you have the correct foreign keys set and everything and you should be fine.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: DataObjectManager Submit from Web Form Link to this post

    I got the first form working, now on my UserRegistrationForm, I see the Submited Data in the Database, but not in CMS

    Here is the DataObject

    class UserApplication extends DataObject {
       static $db = array(
             'FirstName' => 'Text',
             'Surname' => 'Text',
             'Email' => 'Text',
             'CompanyName' => 'Text',
             'CompanyType' => "Text",
             'CompanyURL' => 'Text',
             'HomePhone' => 'Text',
             'Address' => 'Text',
             'AddressLine2' => 'Text',
             'City' => 'Text',
             'Country' => 'Text',
             'TaxIDNumber' => 'Text',
             'Distributor' => 'Text',
             'SalesRep' => 'Text',
             'Notes' => 'Text',
             'ApprovedMember' => 'Boolean',
             'Password' => 'Text'
       );
       
       static $has_one = array(
          'RegistrationPage' => 'RegistrationPage',      
       );
       
       public function getCMSFields_forPopup()
       {
          $companyTypeList = array(
             'Retail' => 'Retail',
             'Online' => 'Online',
             'Distributor' => 'Distributor',
             'Press' => 'Press'
          );
          
          return new FieldSet (
             new CheckBoxField('ApprovedMember', 'Approved Member'),
             new TextField('FirstName', 'First Name'),
             new TextField('Surname','Last Name'),
             new EmailField('Email'),
             new TextField('CompanyName','Company Name'),
             new DropdownField('CompanyType','Company Type',$companyTypeList),
             new TextField('CompanyURL','Company Website'),
             new TextField('HomePhone','Company Phone'),
             new TextField('Address','Address'),
             new TextField('AddressLine2','Address Line 2'),
             new TextField('City','City'),
             new TextField('Country','Country'),
             new TextField('TaxIDNumber','TaxIDNumber'),
             new TextField('Distributor','Distributor'),
             new TextField('SalesRep','Sales Rep'),
             new TextareaField('Notes','Description'),
             new ConfirmedPasswordField('Password')
          );
          
       }

    }

    And the Registration Page

    class RegistrationPage extends Page
    {
       static $has_many = array (
          'UserApplications' => 'UserApplication'
       );
       
       public function getCMSFields()
       {
          $f = parent::getCMSFields();
          $applicationManager = new ApplicationDataObjectManager(
             $this,
             'UserApplications',
             'UserApplication',
             array(
                 'FirstName' => 'FirstName',
                 'Surname' => 'Surname',
                 'Email' => 'Email',
                 'CompanyName' => 'CompanyName',
                 'CompanyType' => "CompanyType",
                 'CompanyURL' => 'CompanyURL',
                 'HomePhone' => 'HomePhone',
                 'Address' => 'Address',
                 'AddressLine2' => 'AddressLine2',
                 'City' => 'City',
                 'Country' => 'Country',
                 'TaxIDNumber' => 'TaxIDNumber',
                 'Distributor' => 'Distributor',
                 'SalesRep' => 'SalesRep',
                 'Notes' => 'Notes',
                 'ApprovedMember' => 'ApprovedMember',
                 'Password' => 'Password'
             ),
             'getCMSFields_forPopup'
          );
          
          $f->addFieldToTab("Root.Content.Applications", $applicationManager);
          
          $f->removeFieldFromTab('Root.Content.Main', 'Content');
          $f->removeFieldFromTab('Root.Content.Main', 'LifestyleText');
          
          return $f;
       }
       
    }

    class RegistrationPage_Controller extends Page_Controller
    {

       /**Create Review Form****
       ************************
       ************************/
       function UserRegistrationForm() {
    // Create fields
        $companyTypeList = array(
             'Retail' => 'Retail',
             'Online' => 'Online',
             'Distributor' => 'Distributor',
             'Press' => 'Press'
          );
    $fields = new FieldSet(
                          new TextField('FirstName', 'First Name'),
                          new TextField('Surname','Last Name'),
                          new EmailField('Email'),
                          new TextField('CompanyName','Company Name'),
                          new DropdownField('CompanyType','Company Type',$companyTypeList),
                          new TextField('CompanyURL','Company Website'),
                          new TextField('HomePhone','Company Phone'),
                          new TextField('Address','Address'),
                          new TextField('AddressLine2','Address Line 2'),
                          new TextField('City','City'),
                          new TextField('Country','Country'),
                          new TextField('TaxIDNumber','TaxIDNumber'),
                          new TextField('Distributor','Distributor'),
                          new TextField('SalesRep','Sales Rep'),
                          new TextareaField('Notes','Description'),
                          new ConfirmedPasswordField('Password')
    );

    // Create actions
    $actions = new FieldSet(
    new FormAction('doApplication', 'Submit')
    );

    return new Form($this, 'UserRegistrationForm', $fields, $actions);
    }

    function doApplication($data, $form) {
    $application = new UserApplication();
    $form->saveInto($application);
    $application->write();
    }
    }

  • dubtje
    Avatar
    Community Member
    17 Posts

    Re: DataObjectManager Submit from Web Form Link to this post

    I have exactly the same problem.
    It seems that when saving, the form don't saves the ...PageID field which is required for linking the records from the DOM to the right page in the CMS.
    I've tried it with a fixed value in a hidden field, but it didn't work.

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Submit from Web Form Link to this post

    Don't forget to tie it to its parent record.

    $application = new UserApplication();
    $form->saveInto($application);
    $application->RegistrationPageID = $SomeID
    $application->write();

  • dubtje
    Avatar
    Community Member
    17 Posts

    Re: DataObjectManager Submit from Web Form Link to this post

    Tnx, works fine now!

    1214 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.