Jump to:

794 Posts in 408 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Checkboxes in widget don't save their checked values

Discuss SilverStripe Widgets.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 4980 Views
  • eternalcheesecake
    Avatar
    Community Member
    5 Posts

    Re: Checkboxes in widget don't save their checked values Link to this post

    Hi Artyom, I was having the same issue with CheckboxField's and HTMLEditorField's on widget editors on SS 2.4.4, and I was really hoping your fix would do the trick. So I applied your patch from ticket 6288. It doesn't work for me because the patched code now throws this:

    [User Error] Uncaught Exception: Object->__call(): the method 'setcmsform' does not exist on 'Widget_LatestBlogPosts'

    The relevant code from the patched file: cms/code/WidgetAreaEditor.php line 52:

          foreach($widgets as $widget) {
             $widget->setCMSForm($this->form); // <-- ????
          }

    Doing a search, there is no setCMSForm() function or CMSForm property anywhere in the entire SS 2.4.4 install. Where does setCMSForm() exist for you?

  • eternalcheesecake
    Avatar
    Community Member
    5 Posts

    Re: Checkboxes in widget don't save their checked values Link to this post

    Hi FungshuiElephant, I realize you wrote your fix over a year ago, but I applied your code to Widget.php to a fresh SS 2.4.4 install and the issue still remains--CheckboxField's still do not save their state properly.

  • maksfeltrin
    Avatar
    Community Member
    6 Posts

    Re: Checkboxes in widget don't save their checked values Link to this post

    A simple solution would be to override Widget Class or Decorated it. You just need to override populateFromPostData() method and set to "0" the Boolean fields that are not passed in $_POST because unchecked. Using onBeforeWrite has the same effect.

    function populateFromPostData($data)
    {

       foreach (DataObject::database_fields($this->class) as $name=>$type) {
          if ( (strpos($type, 'Boolean')!==false) and !isset($data[$name])) {
             $data[$name] = 0;
          }
       }

       parent::populateFromPostData($data);
    }

  • Jeramie
    Avatar
    Community Member
    34 Posts

    Re: Checkboxes in widget don't save their checked values Link to this post

    I just came across this post again when working on a clients older site and it saved my butt yet again. Just saying thanks! The fix that FungshuiElephant posted works for SS 2.3.13 as well.

    4980 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.