Add some html to Zend Forms

32,011

Solution 1

The only way I can think of at the moment is to add a dummy element to the form and remove all decorators except an 'HtmlTag' with the attributes you specified in your question. Removing the decorators means that the actual element will not be rendered - only the HtmlTag decorator will be rendered.

so assuming your form is $form:

$form->addElement(
    'hidden',
    'dummy',
    array(
        'required' => false,
        'ignore' => true,
        'autoInsertNotEmptyValidator' => false,
        'decorators' => array(
            array(
                'HtmlTag', array(
                    'tag'  => 'div',
                    'id'   => 'wmd-button-bar',
                    'class' => 'wmd-panel'
                )
            )
        )
    )
);
$form->dummy->clearValidators();

Note that you want to prevent any validation of the element. This is only one way - there are likely others.

Output:

<div id="wmd-button-bar" class="wmd-panel"></div>

There is a good article describing decorators.

Solution 2

You can create your own view helper libraray--App>View>Helper>PlainTextElemet.php

Create a folder in your library folder that name is App so a folder that name is View so in View create Helper Folder so in Helper folder create a class with PlainTextElement name same following

 class App_View_Helper_PlainTextElement extends Zend_View_Helper_FormElement {

        public function PlainTextElement($name, $value = null, $attribs = null) {
            $info = $this->_getInfo($name, $value, $attribs);
            extract($info); // name, value, attribs, options, listsep, disable
            if (null === $value) {$value = $name;}

            return $value;
          }

    }

Then in libray same above create a class App>Form>Element>PlainText.php

And put folowing code in this class

class App_Form_Element_PlainText extends Zend_Form_Element_Xhtml {

    public $helper='PlainTextElement';

    public function isValid($value){

        return true;
    }
}

Now in your form you can create each html code you like:

$someValue = '<div id="wmd-button-bar" class="wmd-panel"></div>';

        $this->addElement(new App_Form_Element_PlainText('pliantext1', array(
                            'value'=>$someValue,
        )));

Don't forget in your application.ini add fllowing lines too:

 autoloaderNamespaces.app = "App_"
 resources.view.helperPath.App_View_Helper="App/View/Helper"

Solution 3

You can try this way, no config, just one extension class reference: http://www.zfsnippets.com/snippets/view/id/50

<?php

/**
 * Form note element
 *
 * @author Ruslan Zavackiy <[email protected]>
 * @package elements
 */

/**
 * Loads helper Zend_View_Helper_FormNote
 */

class Custom_Form_Element_Note extends Zend_Form_Element_Xhtml
{
    public $helper = 'formNote';
}
?>

then

$companies->addElement('note', 'companyNote', array(
            'value' => '<a href="javascript:;" id="addCompany">Add Company</a>'
        ));

Solution 4

How about using some JQuery:

Something like:

<script language="javascript">
    $(document).ready(function() {
        $('#submit-element').append('<div id="wmd-button-bar" class="wmd-panel"></div>');
    });
</script>
Share:
32,011
bluedaniel
Author by

bluedaniel

Created my own recipe search during uni that gives you ideas based on what ingredients you have lying around. The Guardian newspaper bought it and now I work freelance for them pretty much full time.

Updated on May 22, 2020

Comments

  • bluedaniel
    bluedaniel about 4 years

    Im looking for a simple bit of code that will let me add the following html into my zend form:

    <div id="wmd-button-bar" class="wmd-panel"></div>

    Thats it, it needs to be above my 'method' element in the form but thats it. For such a simple action I cant find any methods that don't involve me learning rocket science (i.e Zend Decorators).

  • Thomas
    Thomas about 14 years
    @bluedaniel: There is no 'method' element in HTML, so I interpreted your questions as wanting to put the div above the form element which contains the method attribute.
  • bluedaniel
    bluedaniel about 14 years
    Sorry your right, I meant a textarea that I have given the id of Method. The requirement is a bunch of inputs and this html to go inside the form above this element.
  • cEz
    cEz about 14 years
    +1 for example to help OP with decorators, rather than just advising to use them (as they were mentioned in the question)
  • bluedaniel
    bluedaniel about 14 years
    yeah Ive come accross that article many times but I just needed this one specific action. Im planning to move to another language as soon as this porject is signed off. Thanks for your help
  • 3ehrang
    3ehrang over 11 years
    this is not work for me and I got this error: Exception caught by form: Plugin by name 'PlainTextElement' was not found in the registry
  • 3ehrang
    3ehrang over 11 years
    with some changes in my view helper it works fine now thanks.
  • axiom82
    axiom82 almost 10 years
    This answer has received some negative votes. Zend_Form_Element_Note is a standard class in Zend (at least Zend 1.x)