8

I'm building a form with a class extending Zend_Form.How can I add an img tag inside the form?I also need to add a class to it and align attribute

This is the final result I want to achieve:

<span class="myElement"><img src="myPath" align="middle" class="myClass"/>
<input type="text"></span>

I didnt find much about Zend_Form_Element_Image's documentation

thanks

Luca

luca
  • 36,606
  • 27
  • 86
  • 125
  • 1
    here is the doc http://framework.zend.com/apidoc/1.9/Zend_Form/Element/Zend_Form_Element_Image.html – Raj May 26 '11 at 11:46
  • Zend_Form_Element_Image creates input type="image" form element. So you want to upload images, or you want to add an image tag (e.g. for decoration) to your form? – Marcin May 26 '11 at 11:58
  • I need img tag.. not an input actually – luca May 26 '11 at 12:12

3 Answers3

10

Have in library/Application/Form/Element/Img.php

class Application_Form_Element_Img extends Zend_Form_Element_Xhtml
{
    public $helper = 'formImg';

    public function loadDefaultDecorators ()
    {
        parent::loadDefaultDecorators ();
        $this->removeDecorator ('Label');
        $this->removeDecorator ('HtmlTag');

        $this->addDecorator('HtmlTag', array (
        'tag'   => 'span',
        'class' => 'myElement',
        ));
    }
}

In application/view/helpers/FormImg.php

class Zend_View_Helper_FormImg extends Zend_View_Helper_FormElement
{
    public function formImg ($name, $value, $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);

        $xHtml = '<img'
                . $this->_htmlAttribs ($attribs)
                . ' />';

        return $xHtml;
    }
}

In your form:

    $this->addElement ('img', 'myimage', array (
        'src'           => '/images/download.png',
        'align'         => 'right',
    ));

Note: paths are subject to change in your particular application.

akond
  • 15,865
  • 4
  • 35
  • 55
3

Actually, you don't need a custom element to do that. You can use HtmlTag decorator and use the openOnly option.

$form = new Zend_Form();

$form->addElement("text", "foo", array("decorators" => array(
    array(array("img" => "HtmlTag"), array(
        "tag" => "img",
        "openOnly" => true,
        "src" => "myPath",
        "align" => "middle",
        "class" => "myClass"
    )),
    array("ViewHelper"),
    array(array("span" => "HtmlTag"), array(
        "tag" => "span",
        "class" => "myElement"
    ))
)));

echo $form->foo;
Nekken
  • 71
  • 2
  • This is better than the accepted answer in my view. Though (I reckon) it's even better to use 'placement' =>'prepend' than to use 'openOnly'=>true – Chris Lear Oct 07 '14 at 15:04
2

Hi you can create a custom element called "html"

class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml
{
    public $helper = 'formHtml';
}

Now you can call it:

$yourForm->addElement(
            'html',
            'myElementId',
            array(
             'value'=>'<span class="myElement"><img src="myPath" align="middle"  class="myClass"/>
<input type="text"></span>'))

For more info you can check this link:
Zend Framework: Insert DIV and Image in my form

Community
  • 1
  • 1
Nikolai Senkevich
  • 2,370
  • 2
  • 19
  • 29
  • It's a great idea in principle but you should never name your own code after a library. In case there's an overlap. I like the approach though, nice and simple. – Ian Lewis Jan 16 '13 at 14:08