Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Dropdown fields state not saved

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: 1050 Views
  • shrike
    Avatar
    Community Member
    15 Posts

    Dropdown fields state not saved Link to this post

    Anyone had a problem with FilteredDropdownSet? I’ve got it listing the categories and items correctly and the selection is saved correctly, but when editing, the dropdowns don’t select the saved item. I suppose it has to do with that AvailableProduct.php thing, but how exactly is the class and the has_ones in it supposed to be named?

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Dropdown fields state not saved Link to this post

    static $has_one = array(
    'Foo' => 'FooClass' // creates a column 'FooID'
    );
    ..

    $fields->push(new DropdownField('FooID', 'Foo'..)

    Make sure you use FooID as the field name not Foo as you're referring to the relationship column value (which is stored as Name + ID)

  • shrike
    Avatar
    Community Member
    15 Posts

    Re: Dropdown fields state not saved Link to this post

    Thanx! Problem actually appears, when you use two drop downs inside FilteredDropdownSet. One dropdown works nicely - but... Idea is to create main categories to dropdown 1 and products based on selected item from dropdown 1 to appear in dropdown 2. Selection works nicely: items are printed nicely on the dropdowns but when hitting the save, products (in drop down 2) is back to default value.

    Hope you got the idea and figured out the problem?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Dropdown fields state not saved Link to this post

    That form field does have one minor drawback, and that's that you have to redundantly store the value of the parent ID, as well. Let's look at an example with Towns and Regions. Towns have a parent Region. A Place is in a Town, and we'll use a FilteredDropdownSet to choose a region first, then the town.

    class Town extends DataObject
    {

       static $db = array (
          'Title' => 'Varchar(255)',
       );

       static $has_one = array (
          'Region' => 'Region',
       );

    }

    class Region extends DataObject
    {

       static $db = array (
          'Title' => 'Varchar(255)',
       );

    }

    Place.php

       static $has_one = array (
          'Region' => 'Region',
    'Town' => 'Town'
       );

    new FilteredDropdownSet(
    array(
    $region = new DropdownField('RegionID','Region',$map_of_regions),
    $town = new DropdownField('TownID','Town',$map_of_towns)
    ),
    "RegionID",
    "Town"
    )

    As you can see, the field RegionID is redundant on the Place object because we know it by transference through the TownID. It's a little annoying, but unfortunately it just works best that way in order to support the native functions of SilverStripe forms.

    ---------------
    Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

  • shrike
    Avatar
    Community Member
    15 Posts

    Re: Dropdown fields state not saved Link to this post

    Many thanks! Works like a charm!

  • Sticks
    Avatar
    Community Member
    29 Posts

    Re: Dropdown fields state not saved Link to this post

    Make sure you use FooID as the field name not Foo as you're referring to the relationship column value (which is stored as Name + ID)

    Thanks Willr. This was my exact issue this morning.

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: Dropdown fields state not saved Link to this post

    Hi,
    Who could help me width FilteredDropdownSet?
    I make like in post and this Field not filtered.

    I have ProCate.php

    class ProCate extends DataObject
    {
    static $db = array (
    'Title' => 'Varchar(255)',
    );

    }

    I have Prod.php

    class Prod extends DataObject
    {
    static $db = array (
    'Title' => 'Varchar(255)',
    );

    static $has_one = array (
    'ProCate' => 'ProCate',
    );
    }

    and AvilableProduct.php

    class AvailableProduct extends DataObject
    {
       static $has_one = array (
       'ProCate' => 'ProCate',
       'Prod' => 'Prod'
    );

    function getCMSFields()
       {
        $fields = parent::getCMSFields();
          
        $cate_map = array();
    $prod_map = array();

    if($result = DataObject::get("ProCate")) {
    $cate_map = $result->toDropdownMap();
    }
    if($result = DataObject::get("Prod")) {
    $prod_map = $result->toDropdownMap();
    }

    $fields->addFieldToTab("Root.Main", new FilteredDropdownSet(array(
        new DropdownField('ProCateID', 'ProCate',$cate_map),
        new DropdownField('ProdID', 'Title', $prod_map)
    ),"ProCateID","Prod"));

          return $fields;
       }

    In BackEnd i have Error
    When I do not want to show the error in backend I must change (add the dot)
    but still not filtered

    new DropdownField('ProCate.ID', 'ProCate',$cate_map),
    new DropdownField('Prod.ID', 'Title', $prod_map)

    In Frontend in Search filtered don't work

    ****

    public function getCustomSearchContext() {
          
           if($result = DataObject::get("ProCate")) {
    $cate_map = $result->toDropdownMap();
    }
    if($result = DataObject::get("Prod")) {
    $prod_map = $result->toDropdownMap();
    }

          $fields = new FieldSet(
          new FilteredDropdownSet(array(
          new DropdownField('ProCateID', 'ProCate', $cate_map),
          new DropdownField('ProdID', Prod', $prod_map)
          ),
          "ProCateID",
          "Prod"
          )
    );
    $filters = array(
    );
    return new SearchContext(
    $this->class,
    $fields,
    $filters
    );
    }

    I must have the filter and I'm sitting on this for several days and probably already tried all the ways and configuration

    Sorry for my bad English

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