silverstripe-display-logic
silverstripe-display-logic copied to clipboard
Issue with multiple forms on one page
If two (or more) forms rendered on one page have a field with the same name, display-logic criteria is added to both - even if one doesn’t have any logic applied.
// Form A
$fields = FieldList::create(
$text = TextField::create('Title', 'Title')
);
$text->displayIf('Title')->isEqualTo('Bar')
// Form B
$fields = FieldList::create(
TextField::create('Title', 'Some unrelated title')
);
The title fields for both forms will get data-display-logic-eval etc added. This is because extensions in SilverStripe are singletons by default, so the $this->displayLogicCriteria array will be stored for all forms: https://github.com/unclecheese/silverstripe-display-logic/blob/c5f807dc24765438b427814350cc884ca70498b0/src/Extensions/DisplayLogic.php#L94-L95
Fix is to add the following YAML config:
SilverStripe\Core\Injector\Injector:
UncleCheese\DisplayLogic\Extensions\DisplayLogic:
type: 'prototype'
Would this mostly happen with front-end forms? How would you get a field with the same name in two forms on one page in the CMS?
Yep this was referring to front-end forms, I haven’t tested recently though