Vinicius Garcia
Vinicius Garcia

Reputation: 1748

ZF 1.11 Form don't show my whitespaces

I want to create a multi-level combobox, like the following example:

<select style="white-space: pre;">
    <option value="0">1 - categoria 0</option>
    <option value="1">&nbsp;1.1 - categoria 1</option>
    <option value="2">&nbsp;&nbsp;1.1.1 - categoria 2</option>
    <option value="3">&nbsp;&nbsp;&nbsp;1.1.1.1 - categoria 3</option>
    <option value="4">&nbsp;&nbsp;&nbsp;&nbsp;1.1.1.1.1 - categoria 4</option>
    <option value="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.1.1.1.1.1 - categoria 5</option>
</select>

The result should be:

1 - categoria 0
 1.1 - categoria 1
  1.1.1 - categoria 2
   1.1.1.1 - categoria 3
    1.1.1.1.1 - categoria 4
     1.1.1.1.1.1 - categoria 5

I'm creating my ZF 1.11 form like this:

class Admin_Form_Category extends Zend_Form
{
    public $elementDecorators2 = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'elementSelect')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );

    public function __construct($options = null)
    {
        //parent::__construct($options);

        $view = new Zend_View();
        $baseUrl = $view->baseUrl();

        // Translating the form
        $translate = Zend_Registry::get('translate');

        $this->setName('formcategory');
        $this->setAttrib('accept-charset', Zend_Registry::get('config')->resources->view->encoding);
        $this->setMethod('post');
        $this->setEnctype(Zend_Form::ENCTYPE_MULTIPART);

        /* HERE IS MY COMBOBOX */
        $parent = new Zend_Form_Element_Select('parent');
        $parent->addErrorMessage($translate->_('You must select an parent'));
        $parent->setLabel($translate->_('Parent'))
            ->setDecorators($this->elementDecorators2)
            ->setRequired(false)
            ->addFilter('StripTags')
                ->setValue( isset($options[ $parent->getName() ]) ? $options[ $parent->getName() ] : '');
        $model = new App_Models_Category();
        $data = $model->fetchAll();

        $parent->addMultiOption('','');
        foreach($data as $row){
                $itemLevel = str_repeat("&nbsp;",$row['level']);
            $parent->addMultiOption($row['category'],$itemLevel.$row['name']);
        }
        $this->addElement($parent);

        /* ... */
    }
}

But, for some reason the combobox is create like this:

<select id="parent" name="parent">
    <option selected="selected" label="" value=""></option>
    <option label="teste" value="14">teste</option>
    <option label="test3" value="16">test3</option>
    <option label="test4" value="17">test4</option>
    <option label="&amp;nbsp;final" value="23">&amp;nbsp;final</option>
    <option label="&amp;nbsp;&amp;nbsp;final2" value="24">&amp;nbsp;&amp;nbsp;final2</option>
</select>

And the result is:

teste
test3
test4
&nbsp;final
&nbsp;&nbsp;final2

Then, the whitespaces are not shown...

Someone can help me?!

Upvotes: 1

Views: 595

Answers (2)

Liyali
Liyali

Reputation: 5693

$this->setAttrib('escape', false); doesn't work with Zend_Form_Element_Select.

Currently, there is no way to do that using a native method since Zend_View_Helper_FormSelect contains a bug already reported here (ZF-9388).

The only solution is to override the formSelect view helper and make it work the way you want. For instance, you would need to write something like this:

$opt = '<option'
             . ' value="' . $value . '"'
             . ' label="' . $label . '"';

instead of this:

$opt = '<option'
             . ' value="' . $this->view->escape($value) . '"'
             . ' label="' . $this->view->escape($label) . '"';

in the _build() method.

Upvotes: 2

dbrumann
dbrumann

Reputation: 17166

By default ZF escapes input from Zend_Form, you can prevent this by adding the following to your Zend_Form_Element:

$this->setAttrib('escape', false);

Upvotes: 0

Related Questions