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');