Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically show/hide field in SilverStripe ModelAdmin

I have a Dataobject in ModelAdmin with the following fields:

class NavGroup extends DataObject {

    private static $db = array(
        'GroupType' => 'Enum("Standard,NotStandard","Standard")',
        'NumberOfBlocks' => 'Int'
    );

    public function getCMSFields() {
        $groupTypeOptions = singleton('NavGroup')->dbObject('GroupType')->enumValues();
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', new Dropdownfield('GroupType', 'Group Type', $groupTypeOptions));
        $fields->addFieldToTab('Root.Main', new Numericfield('NumberOfBlocks', 'Number of Blocks'));
        return $fields;
    }
}

If GroupType == "Standard" I want the NumberOfBlocks field to automatically hide so it's hidden from the user. This should happen dynamically.

Is this functionality available in SilverStripe, or do I need to add some custom JavaScript?

like image 291
BaronGrivet Avatar asked Mar 30 '16 00:03

BaronGrivet


People also ask

What is the SilverStripe admin interface?

The Admin interface is bundled within the Silverstripe CMS but is most commonly used in conjunction with the cms module. The main class for displaying the interface is a specialized Controller called LeftAndMain, named as it is designed around a left hand navigation and a main edit form.

What is modeladmin in SilverStripe CMS?

ModelAdmin provides a simple way to utilize the Silverstripe CMS UI with your own data models. It can create searchables list and edit views of DataObject subclasses, and even provides import and export of your data.

What is leftandmain in SilverStripe?

The main class for displaying the interface is a specialized Controller called LeftAndMain, named as it is designed around a left hand navigation and a main edit form. Starting with Silverstripe CMS 4, the user interface logic is transitioned from jQuery and jQuery.entwine , which is replaced with ReactJS.

Is there a list of supported font icons in SilverStripe CMS?

A complete list of supported font icons is available to view in the Silverstripe CMS Design System Manager ModelAdmin uses the SearchContext class to provide a search form, as well as get the searched results. Every DataObject can have its own context, based on the fields which should be searchable.


1 Answers

You need to use the DisplayLogic module...

https://github.com/unclecheese/silverstripe-display-logic

Then your function can be written as...

public function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldsToTab('Root.Main',array(
        Dropdownfield::create('GroupType', 'Group Type', singleton('NavGroup')->dbObject('GroupType')->enumValues())),
        Numericfield::create('NumberOfBlocks', 'Number of Blocks')
            ->displayIf('GroupType')->isEqualTo('Standard')
    ));

    return $fields;
}
like image 99
Barry Avatar answered Oct 03 '22 06:10

Barry