Aborgrove
Aborgrove

Reputation: 288

Stop rendered Select escaping HTML in Zend Framework 2

Hope this isn't really obvious. I am creating a select element, with multiple options, each option has a mathematical symbol in it.

A simplified version:

<select name="comparison" class="form-control filter-field toggle-display" data-replace-id="from-value"><option value="1">=</option>  
<option value="2">&amp;gt;</option>
<option value="3">&amp;lt;</option>
<option value="4">x &amp;#8804; b &amp;#8804; y</option>
</select>

As you can see the escapeHtmlHelper is doing it job and escaping my html codes. It looks like the is no setting to stop this behaviour and I will need to override the renderOptions method with one of my own, is this correct?

Below is the select creation code

    $filter_model = new \Application\Model\Filter();
    $value_options = $filter_model->getOptions();

    $this->add(array(
        'name' => 'comparison',
        'type'  => '\Zend\Form\Element\Select',
        'attributes' => array(
            'class' => 'form-control filter-field toggle-display',
            'data-replace-id' => 'from-value',
            /*'escape' => false,*/
        ),
        'options' => array(
            'value_options' => $value_options,
        ),
    ));

Filter getOptions:

public function getOptions()
{
    return array(
        self::$EQUALS => '=',
        self::$GREATER_THAN => '&gt;',
        self::$LESS_THAN => '&lt;',
        self::$BETWEEN => "x &#8804; b &#8804; y",
    );
}

Abor.

Upvotes: 0

Views: 461

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

The HTML sample you posted shows symbols that have been double-escaped. Just make sure the symbols in $value_options are not already escaped and your code should work fine.

Upvotes: 1

Related Questions