javascriptcakephpformhelper

Adding inline JavaScript to form input element in CakePHP


I'm trying to do this:

echo $this->Form->input("filter.wijk", array(
    'onChange' => 'this.form.submit()', 
    'type' => 'select', 
    'multiple' => 'checkbox', 
    'options' => $wijkOpties, 
    'label' => false)
);

However, the onChange attribute does not appear in the resulting HTML. The documentation for FormHelper::input tells us this:

* ### Options
*
* See each field type method for more information. Any options that are part of
* $attributes or $options for the different **type** methods can be included in `$options` for input().i
* Additionally, any unknown keys that are not in the list below, or part of the selected type's options
* will be treated as a regular html attribute for the generated input.

Am I interpreting that last sentence in the wrong way?


Solution

  • You have 2 options:

    1- Give up on the checkboxes and simply get a normal select list element/box:

    echo $this->Form->input("filter.wijk", array(
                            'onChange' => 'this.form.submit()', 
                            'type' => 'select', 
                            'multiple' => true, 
                            'options' => $wijkOpties, 
                            'label' => false)
    );
    

    2- Do a workaround for a valid checkboxes list (Something like):

    $i = 0;
    foreach($wijkOpties as $value => $option){
        echo $this->Form->input('filter.wijk.' . $i++, array(
                                    'onChange' => 'this.form.submit()',
                                    'label' => $option,
                                    'value' => $value,
                                    'type' => 'checkbox',
        ));
    }
    

    `