silverstripe-display-logic icon indicating copy to clipboard operation
silverstripe-display-logic copied to clipboard

Issue with multiple forms on one page

Open lozcalver opened this issue 5 years ago • 2 comments

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'

lozcalver avatar Jun 15 '20 11:06 lozcalver

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?

michalkleiner avatar Feb 15 '23 02:02 michalkleiner

Yep this was referring to front-end forms, I haven’t tested recently though

lozcalver avatar Feb 15 '23 09:02 lozcalver