2

I am using cakephp 3 and using Form->input() and need to pass an array of options.

My array looks like this :

$options=['option1', 'option2', 'option3'];

I need the values of these options same as labels. Problem is, cakephp is using array index as value. So if someone selects option1, values is going 0. I need the value to be option1.

Edit :

For now, I've changed the array to look like this :

$options=['option1'=>'option1', 'option2'=>'option2', 'option3'=>'option3'];

It works, but still out of curiosity, is there any other way?

1
  • Can you make array like: $options = array( 'option1'=>'option1', 'option2'=>'option2', 'option3'=>'option3' ); Commented Jan 4, 2017 at 6:08

3 Answers 3

3

Try this:

In Controller

$options = $this->YourModel->find('list', ['keyField' => 'name', 'valueField' => 'name']);

$this->set(compact('options'));

More info about Finding Key/Value Pairs

In View

<?= $this->Form->input('field', ['options' => $options ,'label' => 'Fields']); ?>

or

<?= $this->Form->select('field', $options); ?>

More info about FormHelper

Sign up to request clarification or add additional context in comments.

Comments

1

I wrote a couple of little helper functions for dealing with this situation.

function make_option($value) {
    if (is_numeric($value)) {
        return $value;
    } else {
        // Translate string values
        return __($value);
    }
}

function make_options($values) {
    if (empty($values)) {
        return [];
    }
    return array_combine($values, array_map('App\Config\make_option', $values));
}

You would then use

$options = make_options(['option1', 'option2', 'option3']);

If translation isn't a concern for you, the return from your version of make_options can be simplified to just array_combine($values, $values);, and make_option can go away entirely.

Comments

0

This is because keys of array becomes the value and value of array becomes the label/text for display in select box. Options work based on key-value pairs. e.g.

$options=['option1'=>'option1', 'option2'=>'option2', 'option3'=>'option3'];
echo $this->Form->select(
'field',
[1, 2, 3, 4, 5],
['empty' => '(choose one)']
);

Becomes

<select>
 <option value='option1'>Option1</option>
 ...
 <option value='option2'>Option2</option>


 <select name="field">
   <option value="">(choose one)</option>
   <option value="0">1</option>
   <option value="1">2</option>
   <option value="2">3</option>
   <option value="3">4</option>
  <option value="4">5</option>
 </select>

3 Comments

but it is working now and in question you are asking the reason.
For curiosity purpose, I am asking if we can make it work like that.
no we can not do that. if you does not specify the keys of array then that will fetch default key of array which starts from [0] => 'option1' ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.