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

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

1) how to define additional 'disabled' option?

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

2) How to pre-select element?

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

Thanks!



39.3k
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.

https://docs.phalcon.io/en/latest/api/Phalcon_Tag.html



29.4k

@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>
</select>


29.4k

For multi selection it's easy

$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.phalcon.io/discussion/4475/how-to-selected-in-select-option-using-tag