Zend Framework: Insert DIV and Image in my form
Asked Answered
H

8

3

I need to insert html code like this in my zend form:

<div class="myClass1" id="myId1" style="display: none;"><img src="images/myImage.jpg" /></div>

What will be the code in zend framework for above html code. I tried with Create_Element but it always create a button inside a div tag. Please give an example code. Thanks

Hazan answered 6/8, 2009 at 11:44 Comment(0)
V
8

I created a custom element called "html". I added Html.php to /Form/Element:


/** Zend_Form_Element_Xhtml */
require_once 'Zend/Form/Element/Xhtml.php';

/**
 * HTML form element
 * 
 */
class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formHtml';
}

Second I had to add a view helper FormHtml.php (I put it in application/views/helpers):


/**
 * Abstract class for extension
 */
require_once 'Zend/View/Helper/FormElement.php';

/**
 * Helper to show HTML
 *
 */
class Zend_View_Helper_FormHtml extends Zend_View_Helper_FormElement
{
    /**
     * Helper to show a html in a form
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     *
     * @param mixed $value The element value.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function formHtml($name, $value = null, $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable

        // Render the button.
        $xhtml = 'view->escape($id) . '">'
            . $this->_htmlAttribs($attribs)
            . $this->view->escape($value) . '';

        return $xhtml;
    }
}

You can then add html to your form as follows:


$form->createElement('html', 'someid', array('value'=>'gna

foobar

'));

There might be some more simplifications possible.

Vogue answered 10/10, 2009 at 14:45 Comment(0)
M
4

If you're creating a form element, I think you have to override the isValid() method, as in the code below, or your "value" will disappear upon a validation error:

class RenomoZF_Form_Element_Note extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formNote';

    public function isValid($value, $context = null) {
      return TRUE; 
    }

}
Morphology answered 14/12, 2009 at 13:6 Comment(1)
Thanks so much! This helped me out a ton! Couldn't figure out why my note was disappearing on submit. :)Closer
M
3

You can use the 'note' element type to add html by passing the markup as the element's value.

Molton answered 6/8, 2009 at 13:14 Comment(0)
H
2

This is quasi-finished in ZF 1.10. Seems someone made a Zend_View_Helper_FormNote class so you can insert arbitrary (cept for the decorator) HTML.

To use it you must extend Zend_Form_Element_Xhtml.

<?php

/** Zend_Form_Element_Xhtml */
require_once 'Zend/Form/Element/Xhtml.php';

/**
 * Note form element
 * 
 */
class MyNS_Form_Element_Note extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formNote';
}

Then using the form plugin loader, add your form/element classes to the form object. I put it in my library folder (MyNs/Form/Element/Note.php).

 $yourForm= new Zend_Dojo_Form();
 $yourForm->addPrefixPath("MyNS_Form", "MyNS/Form/");

Now you can call it just like any element.

 $yourForm->addElement(
                'note',
                'myElementId',
                array(
                 'value'=>'<a href="#">omgwtfbbq</a>'
                )
            )

As I mentioned before this still wraps your code in the decorator, but its a solution pre-built into ZF.

Hoover answered 29/6, 2010 at 21:3 Comment(0)
I
1

had the same problem just used the following from tomas.fejfar comment.

array(escape => false)

e.g. 

$decorators = array(
    'ViewHelper',
    'Label',
    array('Description', array('escape' => false,)),
    'Errors')
);

Worked a treat.

Cheers

Isomorph answered 16/6, 2010 at 18:2 Comment(0)
N
0

I usually use description with array(escape => false) option to render HTML in form.

Nic answered 6/8, 2009 at 21:0 Comment(0)
C
0

Have you considered a form ViewScript? See this discussion.

Concretize answered 14/8, 2009 at 16:50 Comment(0)
I
0

With above method the value of FormNote still disappears on submitting the form with validation errors!

Edit: Can be deleted, with overwriting isValid() the problem is gone.

Incarcerate answered 23/5, 2011 at 13:57 Comment(1)
Please pay attention to the date of the original question. This question was nearly two years ago with an accepted answer.Filtrate

© 2022 - 2024 — McMap. All rights reserved.