1

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?

Kunok
  • 8,089
  • 8
  • 48
  • 89

2 Answers2

1

This is working on my system. You can also set it from controller like this :

$this->request->data[$this->modelClass]['PaymentMethods'] = 'ax';

Please check these url also CakePHP select default value in SELECT input
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

example :

$sizes = array('s' => 'Small', 'm' => 'Medium', 'l' => 'Large');
echo $this->Form->input(
    'size',
    array('options' => $sizes, 'default' => 'm')
);
Community
  • 1
  • 1
Akshay Sharma
  • 1,042
  • 1
  • 8
  • 21
1

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
  )); ?>
Kunok
  • 8,089
  • 8
  • 48
  • 89