3

I am trying to create a select input field. However I want to set the values of each individual option manually.

in an attempt I tried the following:

echo $this->Form->input('field', array(
    'options' => array('Active', 'Blocked', 'Pending', 'Unknown'),
    'values' => array(1,2,0,99),
    'empty' => '(choose one)'
));

However this did not help (i.e 'Active' was 0, 'Blocked' was 1 etc...)

Does anyone know if it is possible to manually set the values?

2 Answers 2

7

values is not the right key, you need to leverage the options array for it, as well:

'options' => array(1 => 'Active', 2 => 'Blocked', 0 => 'Pending', 99 => 'Unknown'),

but that is basic PHP (since non-defined keys are numerically indexed starting off at 0).

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

Comments

2

You’ll need to use an associative array to set the keys as well:

$options = array(
    '1' => 'Active',
    '2' => 'Blocked',
    '0' => 'Pending',
    '99' => 'Unknown'
);
echo $this->Form->input('field', array('options' => $options));

However, I’d advise storing options like this in a separate database table rather than hard-coding them, to keep your views DRY and allowing them to be easily modified in the future.

Comments

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.