zend form custom attribute in select option?

前端 未结 6 1076
慢半拍i
慢半拍i 2020-12-29 09:04

I want know how to add custom attribute for option in a select field of Zend form.

PHP:

$option_values = array(\"multiOptions\" => array(
    \"US         


        
6条回答
  •  执笔经年
    2020-12-29 09:42

    I didn't find @mingos's answer complete and had some issues with implementing setting the value. His answer helped a lot with what needed to be extended and changed however. Once I had that start the rest was pretty easy. I just extended ZF1's Select and overrode where I needed:

    /**
     * Select, now with abbility to specify attributes on 

    And the view helper:

    class MyNamespace_View_Helper_SelectAttribs extends Zend_View_Helper_FormElement {

    public function selectAttribs($name, $value = null, $attribs = null, $options = null, $listsep = "
    \n") { $info = $this->_getInfo($name, $value, $attribs, $options, $listsep); extract($info); // name, id, value, attribs, options, listsep, disable // force $value to array so we can compare multiple values to multiple // options; also ensure it's a string for comparison purposes. $value = array_map('strval', (array) $value); // check if element may have multiple values $multiple = ''; if (substr($name, -2) == '[]') { // multiple implied by the name $multiple = ' multiple="multiple"'; } if (isset($attribs['multiple'])) { // Attribute set if ($attribs['multiple']) { // True attribute; set multiple attribute $multiple = ' multiple="multiple"'; // Make sure name indicates multiple values are allowed if (!empty($multiple) && (substr($name, -2) != '[]')) { $name .= '[]'; } } else { // False attribute; ensure attribute not set $multiple = ''; } unset($attribs['multiple']); } // now start building the XHTML. $disabled = ''; if (true === $disable) { $disabled = ' disabled="disabled"'; } // Build the surrounding select element first. $xhtml = '_htmlAttribs($attribs) .">\n "; // build the list of options $list = array(); $translator = $this->getTranslator(); foreach ((array) $options as $optionKey => $optionData) { if (isset($optionData['options'])) { $optDisable = ''; if (is_array($disable) && in_array($optionData['value'], $disable)) { $optDisable = ' disabled="disabled"'; } if (null !== $translator) { $optValue = $translator->translate($optionData['value']); } $optId = ' id="'.$this->view->escape($id).'-optgroup-' .$this->view->escape($optionData['value']).'"'; $list[] = ''; foreach ($optionData['options'] as $optionKey2 => $optionData2) { $list[] = $this->_build($optionKey2, $optionData2, $value, $disable); } $list[] = ''; } else { $list[] = $this->_build($optionKey, $optionData, $value, $disable); } } // add the options to the xhtml and close the select $xhtml .= implode("\n ", $list)."\n"; return $xhtml; } /** * Builds the actual "; return $opt; }

    }

    And implementation in a form would be something like:

    $selectElement = new MyNamespace_Form_Element_SelectAttribs('selectElementName');
    $selectElement->addMultiOption($value, $label, array('data-custom' => 'custom data embedded in option tag.');
    

    I hope that helps someone. Thanks.

提交回复
热议问题