how to que form checklist attributes on template

alnux
Member | 139
+
0
-

Hi there, i dont know how to get attributes from presenter to custom input template

$prod_raw=[
                    '{"1":3.5}'=>"Hielo grande",
                    '{"2":2.5}'=>"Hielo pequeño",
                    '{"3":3.5}'=>"Agua",
                ];

                $this->form->addCheckboxList('productos_raw', 'Producto Raw', $prod_raw)
                ->setAttribute('onChange','insertarProducto(this.value,this.id)');

and this my template

{foreach $form['productos_raw']->items as $key => $item}
<input type=checkbox name="{$form['productos_raw']->name}[]" value={$key}> {$item}

{/foreach}

the question is how to get onchange or another attributes(type, required, etc…) on custom template from present form

Last edited by alnux (2017-05-09 05:50)

duke
Member | 650
+
+1
-

From documentation of forms:

You can render RadioList, Checkbox or CheckboxList by HTML elements individually. This is called partial rendering:

{foreach $form[gender]->items as $key => $label}
    <label n:name="gender:$key"><input n:name="gender:$key"> {$label}</label>
{/foreach}

You can add other attributes either in this template, e.g.:

<input n:name="gender:$key" n:class="example, $iterator->isOdd() ? odd : even" data-mydatafield="somevalue" onchange="whatever();">

… however I suggest against using onchange like this. Better bind these from outside (i.e. use unobtrusive javascript).

Or you can set them from presenter:

$input->getControlPrototype()->setAttribute('class', 'example')->data('mydatafield', 'somevalue');
// or also:
$input->getControlPrototype()->class = 'example';

… but setting it this way is limited if you are working with MultiChoiceControl such as CheckboxList (you can only specify the common attributes for all checkboxes this way). You can still customize them individually in template though.