views:

669

answers:

4

Hello everyone,

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).

A: 

Put it in your view script...

<!-- /application/views/scripts/myController/myAction.phtml -->

<div id="wmd-button-bar" class="wmd-panel"></div>
<?php echo $this->form ;?>
Thomas
How is that inside the form?
bluedaniel
@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.
Thomas
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.
bluedaniel
A: 

You have to add decorator.

Any markup decorator may be helpful.

For further information about decorators see: http://www.slideshare.net/weierophinney/leveraging-zendform-decorators

takeshin
+5  A: 

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 http://devzone.zend.com/article/3450 and once you understand how they work, you'll find it's not rocket science...

jah
+1 for example to help OP with decorators, rather than just advising to use them (as they were mentioned in the question)
Cez
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
bluedaniel
+2  A: 

u 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

so 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,
        )));

dont forget in your application.ini add fllowing lines too

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