Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Checkboxes in widget don't save their checked values


Go to End


12 Posts   9037 Views

Avatar
Roweena

Community Member, 28 Posts

21 April 2009 at 2:08am

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?

Avatar
FungshuiElephant

Community Member, 57 Posts

28 April 2009 at 4:38am

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

Avatar
Roweena

Community Member, 28 Posts

29 April 2009 at 5:41am

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

Avatar
FungshuiElephant

Community Member, 57 Posts

26 May 2009 at 1:31am

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.

Avatar
FungshuiElephant

Community Member, 57 Posts

26 May 2009 at 1:43am

Edited: 26/05/2009 1:57am

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.

Avatar
Jeramie

Community Member, 34 Posts

5 February 2010 at 11:52am

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

Avatar
Artyom

Community Member, 22 Posts

22 December 2010 at 12:43pm

Edited: 22/12/2010 12:45pm

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

Avatar
Artyom

Community Member, 22 Posts

23 December 2010 at 7:54pm

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)

Go to Top