简体   繁体   中英

Prepend Zend form with custom html

I'm writing my first Zend PHP app and I need some help. I'm generating my form through creating class extending Zend_Form and in init() I'm adding elements like:

    $user = $this->addElement('text', 'user', array(

    'filters'    => array('StringTrim'),

    'validators' => array(

            array('StringLength', false, array(3, 30)),

        ),

    'required'   => true,

    'label'      => 'Name',

        'size' => 30 

    ));

Now I need to prepend my form with custom html paragraph ( <p> element) with custom text. How I can do this?

PS: I'm using old ZEND (Before ZF2)

You can insert any piece of html code in the view where you will render the form like this:

file: formView.phtml

----------
<p> Here's my text </p>

<?php echo $this->form; ?>

However, if you want the <p> block to be inside the <form> tag, I would use a viewscript decorator to achieve it.

Add something like this in your form class' init method:

$this->setDecorators(array(
        array('ViewScript', array('viewScript' => 'templates/myForm.phtml'))
    ));

And in the templates/myForm.phtml file you can render your form any way you want. Here's an example:

<?php $form = $this->element; ?>
<form action="<?php echo $form->getAction(); ?>">
      <p> Your text goes here </p>
     <?php echo $form->user; ?>
</form>

I hope I made myself clear. If you have any doubts, don't hesitate to ask

You can read more about the viewscript decorator here http://framework.zend.com/manual/1.12/en/zend.form.standardDecorators.html#zend.form.standardDecorators.viewScript

check out this decorators may be helpful

$this->setDecorators(array(

           'FormElements',
           array(array('data'=>'HtmlTag'),array('tag'=>'p')),
           'Form'

   ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM