Search code examples
cakephpcakephp-2.0cakephp-2.3cakephp-2.1

CakePHP2 - Default value for input - select with option multiple


I have Form input with multiple select options. I am unable to set default values. This is my code:

      <?= $this->Form->input('PaymentMethods', array(
          'type' => 'select',
          'multiple' => true,
          'label' => false,
          'options' => array(
            'cash'=>'cash',
            'invoice'=>'invoice',
            'ax'=>'ax',
            'ca'=>'ca',
            'vi'=>'vi',
            'tp'=>'tp',
            'dc'=>'dc'
          ),
          'default'=>'ax'
      )); ?>

How do I set default values for this input with PHP only?


Solution

  • Since this is multi-choice select, value given must be array. And the key shouldn't be default, I should've used value instead.

      <?= $this->Form->input('PaymentMethods', array(
          'type' => 'select',
          'multiple' => true,
          'label' => false,
          'options' => $options,
          'value'=> $array_of_data_fetched_from_database
      )); ?>