سؤال

I am using Zend Framework in my project. I want to add a description/note to my forms, like

fields marked by * are mandatory

But i didn't found how to add a description to a form and how to use it with decorators.

Any help will be highly appreciated. Thanks.

هل كانت مفيدة؟

المحلول

There are two options:

  • Use a from decorator or
  • Extend Zend_Form_Element to create a custom element

I would go with the latter, since it is very common to add parts of raw html code to forms not only before elements or after, but among them also.

You should do something like this:

class My_Form_Element_Raw extends Zend_Form_Element
{
    protected $raw_html;
    public function setRawHtml($value)
    {
        $this->raw_html = $value;
        return $this;
    }
    public function getRawHtml()
    {
        return $this->raw_html;
    }
    public function render()
    {
        // you can use decorators here yourself if you want, or wrap html in container tags
        return $this->raw_html;
    }
}

$form = new Zend_Form();
// add elements
$form->addElement(
    new My_Form_Element_Raw(
        'my_raw_element', 
        array('raw_html' => '<p class="highlight">fields marked by * are mandatory</p>')
    )
);
echo $form->render();

When extending Zend_Form_Element you dont need to overide setOption/s, getOption/s methods. Zend internally uses set* and get* and protected properties to detect element options like in this case protected $raw_html; and public function setRawHtml($value) and public function getRawHtml() Also naming your property $raw_html will accept both options of 'raw_html' and 'rawHtml' respectively

نصائح أخرى

The easiest way to add additional text to your forms is to just add the appropriate html to the page view:

<div>
    <h4>fields marked by * are mandatory</h>
    <?php echo $this->form ?>
</div>

or use the viewScript decorator to control the entire form experience:

<article class="login">
    <form action="<?php echo $this->element->getAction() ?>"
          method="<?php echo $this->element->getMethod() ?>">
        <table>
            <tr>
                <th>Login</th>
            </tr>
            <tr>fields marked by * are mandatory</tr>
            <tr>
                <td><?php echo $this->element->name->renderViewHelper() ?></td>
            </tr>
            <tr>
                <td><?php echo $this->element->password->renderViewHelper() ?></td>
            </tr>
            <tr>
                <td><?php echo $this->element->submit ?></td>
            </tr>
        </table>
    </form>
</article>

However you can add a description to your form using $form->setDescription() then you can render that description with echo $this->form->getDescription(). It would probably be better to use these methods at the element level along with set and getTag() instead of the form level.

To provide the asterisk clue I just use css:

dt label.required:before {
    content: "* ";
    color: #ff0000;
}

I'm sure you can display any note you want using just css if you want.

 class FormDecorators {
    public static $simpleElementDecorators = array(
        array('ViewHelper'),
        array('Label', array('tag' => 'span', 'escape' => false, 'requiredPrefix' => '<span class="required">* </span>')),
        array('Description', array('tag' => 'div', 'class' => 'desc-item')),
        array('Errors', array('class' => 'errors')),
        array('HtmlTag', array('tag' => 'div', 'class' => 'form-item'))
    );
    }

These are decorators for elements i usually use, they contain prefix with * and also description decorator.

then use code:

$element->setDescription('fields marked by * are mandatory');

Add description to one element,after that You can style description to appear somewhere in the bottomt, I hope this helps, have a nice day.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top