silverstripe-asset-admin icon indicating copy to clipboard operation
silverstripe-asset-admin copied to clipboard

Dropdown doesn't behave the same way inside asset admin

Open AljosaB opened this issue 6 years ago • 3 comments
trafficstars

I have extended SilverStripe\Assets\File with an extra has_one relation named Copyright. I also extended SilverStripe\AssetAdmin\Forms\FileFormFactory with updateFormFields function that adds a DropdownField that has setEmptyString('-'). The problem I'm having is that when saving a file inside assets admin a validation error occurs saying "Please select a value within the list provided. 0 is not a valid option". I inspected POST params and I can see CoprightID value being 0 instead of null. I don't know how that happens, but if I add the same relation to some other random DataObject and edit it inside ModelAdmin for example, the POST value for CopyRightID is null.

SS version: 4.3.1

AljosaB avatar Mar 06 '19 16:03 AljosaB

@AljosaB could you please provide some code snippets so we could reproduce the issue

dnsl48 avatar Apr 01 '19 19:04 dnsl48

FileCopyright.php

<?php

namespace Test\DataObjects;

use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\File;

class FileCopyright extends DataObject
{
    private static $table_name = 'FileCopyright';
    
    private static $db = [
        'Title' => 'Varchar(100)'
    ];
	
    private static $has_many = [
        'Files' => File::class
    ];
}

FileExtension.php

<?php

namespace Test\Extensions;

use Test\DataObjects\FileCopyright;
use SilverStripe\ORM\DataExtension;

class FileExtension extends DataExtension
{   
    private static $has_one = [
        'Copyright' => FileCopyright::class
    ];
}

FileFormFactoryExtension.php

<?php

namespace Test\Extensions;

use Test\DataObjects\FileCopyright;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Tab;

class FileFormFactoryExtension extends Extension
{
    public function updateFormFields(FieldList $fields, $controller, $formName, $context)
    {
        $editor = $fields->fieldByName('Editor');
        if ($editor) {
            $dropdown = DropdownField::create('CopyrightID')
                ->setSource(FileCopyright::get()->map())
                ->setEmptyString('-');
			
            $extra = Tab::create('Extended');
            $extra->push($dropdown);
            
            $editor->push($extra);
        }
    }
}

Config YML

---
Name: myproject
---
SilverStripe\Core\Manifest\ModuleManifest:
  project: app

SilverStripe\AssetAdmin\Forms\FileFormFactory:
  extensions:
    - Test\Extensions\FileFormFactoryExtension

---
After:
  - 'framework/*'
---
SilverStripe\Assets\File:
  extensions:
    - Test\Extensions\FileExtension

image

AljosaB avatar Apr 02 '19 09:04 AljosaB

That's really helpful, thank you!

dnsl48 avatar Apr 02 '19 20:04 dnsl48