zend-framework2zend-form-elementzend-form2

How to not escape html entities in a submit button value?


In my form class I am adding a submit button:

$this->add([
    'name' => 'submit',
    'attributes' => [
        'type' => 'submit',
        'value' => 'Login ▹',
    ],
]);

The output is:

<input name="submit" type="submit" value="Login &amp;#9657;">

Login button with html entity code

How do I stop the value from being escaped?

Edit

Based on @RubenButurca answer, here is the output:

Login button with extra html entity code


Solution

  • It felt odd escaping the value of my submit input. When I tried values such as <i class="fa fa-caret-right"></i> it, well, didn't escape the quotes and I ended up with random attributes in my input element. As such, I've swapped from an input field to a button.

    $this->add([
        'name' => 'submit',
        'type' => 'button',
        'attributes' => [
            'type' => 'submit',
            'required' => 'required',
            'value' => 'Login <i class="fa fa-caret-right"></i>',
        ],
        'options' => [
            'label_options' => [
                'disable_html_escape' => true,
            ],
        ],
    ]);
    

    ZendButon requires a label, which I didn't want. So I extended the view helper to take the label value from the element value ($buttonContent). Because there is no label attribute there is no label echoed, but the escaped value is still shown in the button tags.

    FormButton:

    use Zend\Form\View\Helper\FormButton as ZendFormButton;
    use Zend\Form\ElementInterface;
    
    class FormButton extends ZendFormButton
    {
        /**
         * Invoke helper as functor
         *
         * Proxies to {@link render()}.
         *
         * @param  ElementInterface|null $element
         * @param  null|string           $buttonContent
         * @return string|FormButton
         */
        public function __invoke(ElementInterface $element = null, $buttonContent = null)
        {
            if (!$element) {
                return $this;
            }
    
            // New code here
            if(is_null($buttonContent)) {
                $buttonContent = $element->getValue();
            }
            return $this->render($element, $buttonContent);
        }
    
    }
    

    Output:

    <button type="submit" name="submit" value="Login <i class=&quot;fa fa-caret-right&quot;></i>">Login <i class="fa fa-caret-right"></i></button>