cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR]: No way to add html attributes to Select Tag. documentation inadequate

Open zhangster opened this issue 3 years ago • 3 comments

This seems like a really basic feature that's missing. for instance, i can't even have a select tag with css classes since there's no way to add attributes to the tag. somehow you can add classes to other html tags? this needs to be more consistent

zhangster avatar Jan 19 '23 22:01 zhangster

@zhangster how you use select tag, couse i see in documentation way to add more attributes... (maybe you talk about version phalcon 5?) In doc (see options):

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Select;

$escaper = new Escaper();
$helper  = new Select($escaper);

$options = [
    'id' => 'carsList',
];
 
$result = $helper('    ', PHP_EOL, $options);
$result
    ->add("Ferrari", "1", ["class" => "active"])
    ->add("Ford", "2")
    ->add("Dodge", "3")
    ->add("Toyota", "4")
    ->optGroup(
        'oneLabel',
        [
            'class' => 'form-input',
        ]
    )
    ->addPlaceholder(
        'Choose & Car...',
        "0",
        [],
        true,
    )
    ->selected("3")
;

echo $result;
//
//    <select id="carsList">
//        <optgroup class="form-input" label="oneLabel">
//            <option value="0">Choose & Car...</option>
//            <option value="1" class="active">Ferrari</option>
//            <option value="2">Ford</option>
//            <option value="3" selected="selected">Dodge</option>
//            <option value="4">Toyota</option>
//        </optgroup>
//    </select>"

In zep file: https://github.com/phalcon/cphalcon/blob/master/phalcon/Html/Helper/Input/Select.zep

borisdelev avatar Jan 20 '23 13:01 borisdelev

This is available using either the TagFactory or the Forms depending on your case:

$select = $tagFactory->inputSelect();
$result = $select('    ', PHP_EOL, ['id' => 'cars', 'class' => 'form-control'])
    ->add('Ferrari', '1')
    ->add('Ford', '2')
    ->selected('1');

echo $result;
// <select id="cars" class="form-control">
//     <option value="1" selected="selected">Ferrari</option>
//     <option value="2">Ford</option>
// </select>

or

$select = new Select(
    'cars',
    ['1' => 'Ferrari', '2' => 'Ford'],
    ['class' => 'form-control', 'id' => 'cars']
);

echo $select->render();
// <select id="cars" class="form-control" name="cars">
//     <option value="1">Ferrari</option>
//     <option value="2">Ford</option>
// </select>

Resolved in #16999 #16995 #16985 #16950 #16895 / #16893

niden avatar May 13 '26 22:05 niden

Thank you @borisdelev and @zhangster

niden avatar May 13 '26 22:05 niden