Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 ```php $element = new Select('id', MyElement::find(), array( 'using' => array('id', 'name') )); ``` which produces ```html <select> <option value="1">One</option> <option value="2">Two</option> </select> ``` 1) how to define additional 'disabled' option? ```html <select> <option disabled>Please select...</option> <option value="1">One</option> <option value="2">Two</option> </select> ``` 2) How to pre-select element? ```html <select> <option value="1">One</option> <option selected value="2">Two</option> </select> ``` Thanks!



18.8k
Accepted
answer

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.

http://docs.phalconphp.com/en/latest/api/Phalcon%5Tag.html



18.0k

@niden what about this case? ```html <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> </select> ```



18.0k

For multi selection it's easy ```php $this->tag->select([ 'id', MyElement::find(), '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?

Thanks!

edited Mar '16

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

https://forum.phalconphp.com/discussion/4475/how-to-selected-in-select-option-using-tag