-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select is always multiple #424
Comments
Can you provide a concise runnable example? There is something important happening in your selectAttribs helper plugin that may be causing your problem. When I run your code (after commenting out selectAttribs and editing the call to addOption), I do not get a multi-select, but the result is clearly not what you are looking for either. Seems like your problem is in your code. <?php
class My_Form_Element_SelectAttribs extends Zend_Form_Element_Select {
public $options = array();
// What is this?
//public $helper = 'selectAttribs';
public function addOption($value, $label = '', $attribs = array()) {
$value = (string) $value;
if (!empty($label)) {
$label = (string) $label;
} else {
$label = $value;
}
$this->options[$value] = array(
'value' => $value,
'label' => $label
) + $attribs;
return $this;
}
}
$select = new My_Form_Element_SelectAttribs('id');
$select->setLabel('Select')
->setValue(isset($_POST['id']) ? $_POST['id'] : null)
->setAttribs(array(
'size' => 1
));
$select->addOption(false, '==Select==');
$itemsForSelect = [
(object)['country_id' => 1, 'title' => 'Austria', 'something' => 'foo'],
(object)['country_id' => 2, 'title' => 'Belgium', 'something' => 'bar'],
(object)['country_id' => 3, 'title' => 'Croatia', 'something' => 'baz']
];
foreach ($itemsForSelect as $item)
{
/*
Are $select->title and $select->something correct? Should probably be looking for elements of $item, right?
$select->addOption($item->country_id, $select->title, array('data-something' => $select->something));
*/
$select->addOption($item->country_id, $item->title, array('data-something' => $item->something));
}
$form = new Zend_Form('foo');
$form->addElement($select);
echo $form->render();
echo PHP_EOL; Result: <form enctype="application/x-www-form-urlencoded" action="" method="post">
<dl class="zend_form">
<dt id="id-label"><label for="id" class="optional">Select</label></dt>
<dd id="id-element">
<select name="id" id="id" size="1">
<optgroup id="id-optgroup-" label="">
<option value="value"></option>
<option value="label">==Select==</option>
</optgroup>
<optgroup id="id-optgroup-1" label="1">
<option value="value">1</option>
<option value="label">Austria</option>
<option value="data-something">foo</option>
</optgroup>
<optgroup id="id-optgroup-2" label="2">
<option value="value">2</option>
<option value="label">Belgium</option>
<option value="data-something">bar</option>
</optgroup>
<optgroup id="id-optgroup-3" label="3">
<option value="value">3</option>
<option value="label">Croatia</option>
<option value="data-something">baz</option>
</optgroup>
</select></dd>
</dl>
</form> |
Your are right. Helper looked like:
I had to add remove multiple attr when it was set as false:
|
Hi,
I extended Zend_Select:
This is using in form:
Select has multiple attrib. How can I remove it? Param size does not work. In last oficial version of ZF1 ii worked.
The text was updated successfully, but these errors were encountered: