2

Here is my code

    private $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),

    );
     public function init()
     {
      $username  = new Zend_Form_Element_Text('username',array(
            'decorators'    =>$this->elementDecorators,
            'label'         =>'Username',
            'required'      =>true,
            'span'          =>array('class'=>'validation','id'=>'unameInfo'),

        ));
    }

       $this->addElements(array(
                        $username
        ));
        $this->setDecorators(array(
           'FormElements',
            array('HtmlTag',
                array('tag'=>'table', 'width' => '100%')
                ),
               'Form'
        ));

Form created for above code is as below

   <tr>
       <td id="username-label"><label for="username" class="blue-color required">Username</label></td>
       <td><input type="text" name="username" id="username" value="" span="Array"></td>
   </tr>

I want following html

<tr>
           <td id="username-label"><label for="username" class="blue-color required">Username</label></td>
           <td>
               <input type="text" name="username" id="username" value="" span="Array">
               <span class="validation" id="userinfo"></span>
           </td>
</tr>

How can i add span tag in my above zend form code?

Thank you in advance

2

2 Answers 2

0

You can use the AnyMarkup decorator.

$username  = new Zend_Form_Element_Text('username',array(
     'decorators'    => array(
        'ViewHelper',
         array('AnyMarkup', array('markup' => 'your-markup-here', 'placement' => 'append')),
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
     ),
    'label'    => 'Username',
    'required' => true,
    'span'     => array('class'=>'validation','id'=>'unameInfo'), 
    // actually, this last 'span' entry strikes me as odd
));

To add the decorator to an element using the short-form (as above) rather than creating an instance, you need to register the decorator's path/prefix with the element, something like:

$username->addPrefixPath('My_Decorator_', APPLICATION_PATH . '/../library/My/Decorator', Zend_Form_Element::DECORATOR);

You can add that prefix/path to all (currently defined) elements using the:

$form->addElementPrefixPath($prefix, $path)

method.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply !!! I am new in zend framework. So I dont know how to use 'markup'.
Create a file library/My/Decorator/AnyMarkup.php and past in the code from here. Then the code above should largely work.
0

Try like below,

...
     $submit = new Zend_Form_Element_Submit('submit', array(
                    'label' => 'Submit Button',
                    'class' => 'form-submit',
                    'decorators' => array(
                        'ViewHelper',
                    ),
                ));

        //$submit->removeDecorator('Label');
        $this->addElement($submit);

        $reset = new Zend_Form_Element_Reset('reset', array(
                    'label' => 'Reset Button',
                    'class' => 'form-reset',
                    'decorators' => array(
                        'ViewHelper',
                    ),
                ));
        //$submit->removeDecorator('Label');
        $this->addElement($reset);

        $this->addDisplayGroup(array('submit', 'reset',), 'submitButtons', array(
            'order' => 10,
            'decorators' => array(
                'FormElements',
                array(
                    array('data' => 'HtmlTag'),
                    array('tag' => 'td','class'=>'move_td')
                ),
                array(
                    array('row' => 'HtmlTag', 'class' => 'element'),
                    array('tag' => 'tr')
                )
            ),
        ));
...

This will generate code like below,

...
<td class="move_td">

<input type="submit" class="form-submit" value="Submit Button" id="submit" name="submit">

<input type="reset" class="form-reset" value="Reset Button" id="reset" name="reset">
</td>
...

Updated

Use below code to generate span

$this->addElement(
        'hidden',
        'dummy',
        array(
            'required' => false,
            'ignore' => true,
            'autoInsertNotEmptyValidator' => false,
            'decorators' => array(
                array(
                    'HtmlTag', array(
                        'tag'  => 'span',
                        'id'   => 'span-id',
                        'class' => 'span-class'
                    )
                )
            )
        )
    );
    $this->dummy->clearValidators(); 

Now you need to use addDisplayGroup function to group html elements.

3 Comments

Thanks for your reply but I want span tag instead of input type reset
How can I use this addDisplayGroup function with input type text and span? Can you explain this please?
@SwapnilTayade can you add class name next to requires inside addElement function pass array class name of both element to addDisplayGroup() function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.