Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Treemultiselect field and List box ( multiple select )

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

Page: 1
Go to End
Author Topic: 1486 Views
  • Arun
    Avatar
    Community Member
    8 Posts

    Treemultiselect field and List box ( multiple select ) Link to this post

    Hi All,

    In one of my form ( a separate module) i have one Treemultipleselect field. In this field i have to show the Site Contents Tree Structure. Also i have to insert the selected page ids in to the db and while retrieving the inserted record i have to show the selected ids are checked. How can achieve this...?

    Also in the same module i have one List box ( multiple select ). After insertion I have checked in the db and found that for the listbox field its shown as "Array". So my question is how to insert the selected listbox items in to db and how we can show its selected when we retrieve this record for editing...?

    The above 2 questions are almost same... but i really need a help for the first one. I think any of you have the same issue and sorted out. If so please help me for implementing the above two....

    Any help would be much appreciated....

    Thanks
    Arun.

  • Katy
    Avatar
    Community Member
    4 Posts

    Re: Treemultiselect field and List box ( multiple select ) Link to this post

    I solved this as follows:

    I wanted to allow registrants members to indicate which key "Regions" they were interested in.

    In the form I included this code to initialise the list box:

          $member = Member::CurrentMember();

          // initialise field values so already selected regions are shown in list box
          if($member) {

             // initialise Region Values
             $wherevalue = 'Member_Regions.MemberID=' . $member->ID;
             $memberregions = DataObject::get('Region',$wherevalue,'',"LEFT JOIN `Member_Regions` ON `Region`.`ID` = `Member_Regions`.`RegionID`");
             $memberregionsIDs = array();
             if($memberregions) {
                foreach ($memberregions as $memberregion) {
                   $memberregionsIDs[] = (string)$memberregion->ID;
                }
             }
          } else {
                $memberregionsIDs[] = '';
          }

          // Initialise Region Title
          $regionTitle = 'Select one or more Regions below:';
          
          // create field set
          $region = DataObject::get('Region');
          if ($region) {
             $regionsize = ($region->Count()+1);
             $region = $region->toDropdownMap('ID', 'Title', '(Select one or more options)', true);
          }

          $fields->push(new ListBoxField(
             $name = 'regionIDs',
             $title = $regionTitle,
             $source = $region,
             $value = $memberregionsIDs,
             $size = 10,
             $multiple = true
          ));

    The form data selected in the list box was then processed as follows:

          // Create a new Member object and load the form data into it
          $newmember = new Member();

          // Check if new entry is for logged-in member
          if($member = Member::CurrentMember()) {
             if($newmember->ID = $member->ID) {
                $newmember = $member;
             }
          }

             $regions = $data['regionIDs'];

             // remove existing Regions for this Member
             
             $wherevalue = 'Member_Regions.MemberID=' . $newmember->ID;
             $memberregions = DataObject::get('Region',$wherevalue,'',"LEFT JOIN `Member_Regions` ON `Region`.`ID` = `Member_Regions`.`RegionID`");
             if($memberregions) {
                foreach ($memberregions as $memberregion) {
                      $newmember->Regions()->remove($memberregion);
                }
             }

             // add new Regions for this Member

             if($regions) {
                foreach ($regions as $region) {
                      $regionID = (int)$region;
                      $newmember->Regions()->add($regionID);
                }
             }      
             
             $newmember->write();

    1486 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.