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.

Form Questions /

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

[Solved] setAttribute on radio buttons


Go to End


2359 Views

Avatar
Antony

Community Member, 4 Posts

10 November 2014 at 6:03pm

Edited: 11/11/2014 5:52pm

Hi
Is it possible to add attributes to radio buttons? I am updating a module's form to add front-end validation. Am using knockoutjs so wish to add:
data-bind="checked: selected"
to each input.

class PaymentMethodExtension extends DataExtension {
function updatePaymentMethodForm(&$form){
$fields = $form->Fields();
$knockout = "checked: selected"; // the attribute to add to each input

// below doesn't work
$fields->dataFieldByName('PaymentMethod')->setAttribute("data-bind", $knockout);

var_dump($fields->dataFieldByName('PaymentMethod')); // can see OptionsetField
var_dump($fields->dataFieldByName('PaymentMethod')->getAttributes()); // can see the existing attributes, type, name, value, id, class, etc
}
}

I wish to end up with:
<input data-bind=“checked: selected” id="PaymentMethod_CreditCard” class="radio" name="PaymentMethod" type="radio" value="CreditCard" >
<input data-bind=“checked: selected” id="PaymentMethod_DirectCredit” class="radio" name="PaymentMethod" type="radio" value="DirectCredit" >

How do I get data-bind=“checked: selected” into each radio button please?

Thanks in advance.

Update:
An attribute cannot be added to radio button with the setAttribute method. However, I adapted the OptionsetField.ss from the Framework and saving it as a new name under the theme's templates/forms folder. Then pointed the form to the new template: $fields->dataFieldByName('PaymentMethod')->setTemplate('OptionsetFieldKnockout');