unclecheese / silverstripe-display-logic

The Display Logic module allows you to add conditions for displaying or hiding certain form fields based on client-side behavior.
BSD 3-Clause "New" or "Revised" License
74 stars 71 forks source link

Issue with multiple forms on one page #115

Open kinglozzer opened 4 years ago

kinglozzer commented 4 years ago

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'
michalkleiner commented 1 year ago

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?

kinglozzer commented 1 year ago

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