We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Additional options for SELECT element

Hi, what is the best way to define additional 'disabled' option for SELECT tag and also 'selected' attribute for the option? Say, I have select element

$element = new Select('id', MyElement::find(), array(
            'using' => array('id', 'name')

which produces

    <option value="1">One</option>
    <option value="2">Two</option>

1) how to define additional 'disabled' option?

    <option disabled>Please select...</option>
    <option value="1">One</option>
    <option value="2">Two</option>

2) How to pre-select element?

    <option value="1">One</option>
    <option selected value="2">Two</option>



You can do this:

$params = array(
            'useEmpty' => true,
            'emptyText' => 'Please Select...',
            'using' => array('id', 'name')

$element = new Select('id', MyElement::find(), $params);

// Set the value for the field
Select::displayTo('id', '2');

Thanks Nikolaos! Is there any notes about this options in official documentation?

The displayTo is in the documentation but I see that we missed the useEmpty and emptyText. We need to add it there.



@niden what about this case?

<select multiple="multiple">
    <option          value="1">One</option>
    <option disabled value="2">Two</option>
    <option disabled value="3">Three</option>
    <option selected value="4">Four</option>
    <option selected value="5">Five</option>


For multi selection it's easy

    'using'    => ['id', 'name'],
    'value'    => [4, 5],
    'multiple' => 'multiple'

Is it possible to disable some options?

@AgentJ That one is not implemented as far as I can see.

Do you mind opening a NFR on Github so that we can address this?


edited Mar '16

This solution is not valid anymore, answer from this topic actually works (at least in the beginning of 2016):