silverstripe / silverstripe-widgets

Widgets subsystem for Silverstripe CMS
http://silverstripe.org
BSD 3-Clause "New" or "Revised" License
38 stars 55 forks source link

ListboxField not showing selected values #182

Closed jasjitCheema closed 4 years ago

jasjitCheema commented 4 years ago

Steps to follow:-

  1. Create custom widget, in which "FieldList" is having "ListboxField"
  2. Add widget in page and try to select multiple entries from ListboxField. After saving those selected values are gone.

*Checked in Database and these values are getting saved in string form eg. ["45","50"]

Please let me know, if any idea

robbieaverill commented 4 years ago

Would you mind sharing the code for your custom widget please?

jasjitCheema commented 4 years ago

Yes! Sure

robbieaverill commented 4 years ago

I don't have Skype. If possible, you could remove any sensitive code from the file and post it here or in a GitHub Gist. If not, you can reach me on the Silverstripe Community Slack channel.

jasjitCheema commented 4 years ago

Here is the come to create new Widget:-


<?php

namespace Widgets\NewsUpdates;

use SilverStripe\Widgets\Model\Widget;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\ListboxField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\DropdownField;

use News\NewsDetailPage;

class NewsUpdates extends Widget{
    private static $db = array(
        'WidgetTitle' => 'Varchar',
        'NewsPagesId_A'     =>  'Text',
        'WidgetWidth'   =>  'Varchar',
        'ViewAll'       =>  'Varchar'
    );
    private static $title = 'NewsUpdates HALF';
    private static $cmsTitle = 'NewsUpdates';
    private static $description = 'Description goes here';

    private static $many_many = [
        //'NewsPagesId' => NewsDetailPage::class,
    ];

    public function getCMSFields(){
        $news_detail = NewsDetailPage::get()->map('ID','Title')->toArray();
        //print_r( $news_detail );
        //print_r($this->NewsPagesId_A);exit;
        //$news_detail = array("1"=>"one","2"=>"two","3"=>"three");

        $f = new FieldList(
            new TextField('WidgetTitle', 'Title'),
            new TextField('ViewAll', 'View all link'),
            ListboxField::create('NewsPagesId_A','Select newsfff',$news_detail),
            DropdownField::create('WidgetWidth','Widget Width',[
                '12'    =>  'FULL',
                '6'     =>  'HALF',
                '4'     =>  '1/3'
            ])
        );
        return $f;

    }

}
jasjitCheema commented 4 years ago

I don't have Skype. If possible, you could remove any sensitive code from the file and post it here or in a GitHub Gist. If not, you can reach me on the Silverstripe Community Slack channel.

Okay! Thanks for your response

jasjitCheema commented 4 years ago

Any luck?

jasjitCheema commented 4 years ago

Got my mistake. To fix this i have to use "many many" relationship.

private static $many_many = [
    'NewsPagesId' => NewsDetailPage::class,
];

Add following cms field:-

ListboxField::create('NewsPagesId','Select news',$news_detail, **$this->NewsPagesId()** )

dhensby commented 4 years ago

closed as not a bug, then