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: 4974 Views
  • Roweena
    Avatar
    Community Member
    28 Posts

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

    I'm trying to extent the paypal widget to include 3 checkboxes. I have added them to the CMS screen ok, but when I tick them this isn't saved. On looking at the cms page code (View Source) I can see that the checkboxes don't actually have a "value" attribute, eg:
    <input type="checkbox" id="Widget[3][CheckboxValue]" name="Widget[3][CheckboxValue]" />

    My code is as follows...

    class PayPalWidget extends Widget {
       static $db = array('Currency' => 'Varchar(5)', 'Business' => 'Varchar', 'Text' => 'Varchar(255)', 'Amount' => 'Varchar', 'ButtonText' => 'Varchar', 'CheckboxValue' => 'Boolean', 'CheckboxValue2' => 'Boolean', 'CheckboxValue3' => 'Boolean'
    );
       
       static $defaults = array('Currency' => 'GBP', 'Text' => 'Pay with PayPal', 'Amount' => 'Amount to Pay', 'ButtonText' => 'Pay Online');
       
       static $title = "";
       static $cmsTitle = "Paypal button";
       static $description = "Adds a button that allows users to pay with PayPal";
       
       function getCMSFields() {
          return new FieldSet(
             new TextField('Currency', _t('PayPalWidget.CURRENCY', 'The currency the donation should be in'), '', 5),
             new TextField('Business', _t('PayPalWidget.BUSINESS', 'Your PayPal account')),
             new TextField('ButtonText', _t('PayPalWidget.BUTTONTEXT', 'Button text')),
             new TextField('Amount', _t('PayPalWidget.AMOUNT', 'Amount to Pay')),
             new TextField('Text', _t('PayPalWidget.TEXT', 'A short description of the payment that is displayed on your statement.')),
             new CheckboxField('CheckboxValue', _t('PayPalWidget.CHECKBOXVALUE', 'Add extra info box')),
             new CheckboxField('CheckboxValue2', _t('PayPalWidget.CHECKBOXVALUE2', 'Add Chain Size Option')),
             new CheckboxField('CheckboxValue3', _t('PayPalWidget.CHECKBOXVALUE3', 'Add Ring Size Option')));
       }

    Can anyone see what's missing?

  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

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

    The code looks reasonable to me.
    I just tried a very simple widget with a single checkbox and it didn't work either, the field in the db is always 0. Not a solution but at least confirmation that it can be reproduced. Interestingly if you add a checkbox to a simple page (not a widget) it works and saves correctly.

    Looking through the HTML output it looks like we have the duplicate ids for the input field and it's surrounding paragraph. The basic problem is that the checkboxField doesn't know what form it's on which is what causes the duplicate ids. It's not easy to fix and requires changing the SS code. It's related to bug 2836. Bottom line is that widgets with input fields in the CMS cause problems, they're fine in the published website though, so widgets like the login widget work fine.

    I think you're right about the value attribute BTW, I thought that it was mandatory for checkboxes and radio buttons. (It's not causing this problem though.)

  • Roweena
    Avatar
    Community Member
    28 Posts

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

    Ok thanks for the info, at least that saves me spending any more time trying to fix something that can't be!

  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

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

    While investigating a different problem I came across the cause of the duplicate IDs:

    In the CMSEditor function of the Widget.php class (silverstripe/sapphire/widget) there is this regular expression based string replacement:

    $renderedField = ereg_replace("name=\"([A-Za-z0-9\-_]+)\"", "name=\"Widget[" . $this->ID . "][\\1]\"", $renderedField);
    $renderedField = ereg_replace("id=\"([A-Za-z0-9\-_]+)\"", "id=\"".$this->id()."Widget[" . $this->ID . "][\\1]\"", $renderedField);

    It's a bit heavy handed and replaces more than it should IMHO.

    I think I just commented it out and put the following before the $renderedField = $field->FieldHolder(); line:

    $field->setName("Widget[" . $this->ID . "][".$field->Name()."]");


    which is a bit more in keeping with the way SS does forms/fields.

    Warning: I probably changed a ton of other stuff as well but haven't the time to test this patch in isolation at the moment. It should also be said that changing the core files is a really bad idea because the changes will be wiped out following an upgrade so you should override this method in your derived Widget class instead.

    Hope that helps someone, report back if this works for you.

  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

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

    I just checked on a clean install, it works for me.
    I did get a Javascript error at one point but can't reproduce it, so I don't think the two are related.
    Happy checkboxing.

    This might break other Widgets so be sure to test.

  • Jeramie
    Avatar
    Community Member
    34 Posts

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

    This fix also works to correct the issue with Tiny MCE in the HTML Content Widget. I do not currently have any other widets installed, so I was not able to determine how it affects other widgets. This is with SS 2.3.4

  • Artyom
    Avatar
    Community Member
    22 Posts

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

    It was a bug in Widget processing, actually unrelated to the duplicate ID's issue mentioned above. (Though my patch includes his fix for that as well, since that caused another problem)

    Ticket created against 2.4.3 with patches attached

    http://open.silverstripe.org/ticket/6288

  • Artyom
    Avatar
    Community Member
    22 Posts

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

    I just confirmed that my patch (which also contains the suggested fix above) also fixes the html editor issue.

    The one caveat is that you have to SAVE THE PAGE every time you move the widget with the drag capability. (Otherwise the js DOM stuff seems to get confused and you get the same errors that you used to get before)

    4974 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.