描述不显示zf2表单元素

时间:2014-03-11 22:42:01

标签: zend-framework2

我正在尝试向表单元素添加描述。我期待在p标签中获得我的描述。

 <?php
 namespace CsnCms\Form;

 use Zend\Form\Form;

 class ArticleForm extends Form
{
public function __construct($name = null)
{
    parent::__construct('article');
    $this->setAttribute('method', 'post');

    $this->add(array(
        'name' => 'currency',
        'attributes' => array(
            'type'  => 'text',
            'placeholder' =>'Currency',
        ),
        'options' => array(
            'label' => ' ',
            'description' => 'Currency code: ie. USD',
        ),
    ));
}
}

不幸的是,我只是将其作为输出

<label><span> </span><input name="currency" type="text" placeholder="Currency" value=""></label>

任何建议?

3 个答案:

答案 0 :(得分:0)

也许这会起作用     

$form = $this->form;

$element = $form->get("currency");

echo $this->formInput($element);

echo $element->getOption("description"); // <-------

?>

答案 1 :(得分:0)

zf2表单元素没有本机描述选项你需要手动添加它:

$this->add(array(
    'name' => 'catMachineName',
    'type' => 'Zend\Form\Element\Text',
    'options' => array(
        'label' => 'Category Machine Name',
        'description' => 'Machine name can only contain English Alphabets and Numbers and _ and no space and no number at the beginning'
    ),

));

并将其呈现在视图中,如下所示:

echo $form->get('catMachineName')->getOption('description);

答案 2 :(得分:0)

谢谢我写了自己的视图助手。我还在错误标记中添加了一个class =“error”。

感谢帮助人员。

 <?php


    namespace User\View\Helper;

    use Zend\View\Helper\AbstractHelper;
    use Zend\Form\View\Helper\FormRow;
    use Zend\Form\ElementInterface;

    class FormRowWithDescription extends FormRow
    {

        /**
         * Utility form helper that renders a label (if it exists), an element and errors
         *
         * @param  ElementInterface $element
         * @throws \Zend\Form\Exception\DomainException
         * @return string
         */
        public function render(ElementInterface $element)
        { $escapeHtmlHelper    = $this->getEscapeHtmlHelper();
            $labelHelper         = $this->getLabelHelper();
            $elementHelper       = $this->getElementHelper();
            $elementErrorsHelper = $this->getElementErrorsHelper();

            $label           = $element->getLabel();
            $inputErrorClass = $this->getInputErrorClass();

            if (isset($label) && '' !== $label) {
                // Translate the label
                if (null !== ($translator = $this->getTranslator())) {
                    $label = $translator->translate(
                        $label, $this->getTranslatorTextDomain()
                    );
                }
            }

            // Does this element have errors ?
            if (count($element->getMessages()) > 0 && !empty($inputErrorClass)) {
                $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '');
                $classAttributes = $classAttributes . $inputErrorClass;

                $element->setAttribute('class', $classAttributes);
            }

            if ($this->partial) {
                $vars = array(
                    'element'           => $element,
                    'label'             => $label,
                    'labelAttributes'   => $this->labelAttributes,
                    'labelPosition'     => $this->labelPosition,
                    'renderErrors'      => $this->renderErrors,
                );

                return $this->view->render($this->partial, $vars);
            }

            if ($this->renderErrors) {

                $elementErrorsHelper
                     ->setMessageOpenFormat('<ul%s><li class="error">')
                     ->setMessageSeparatorString('</li><li class="error">');
                $elementErrors = $elementErrorsHelper->render($element);
            }



            $elementString = $elementHelper->render($element);




            $description = $element->getOption('description');
            $elementString.="<p class='description'>".$description."</p>";

            if (isset($label) && '' !== $label) {
                $label = $escapeHtmlHelper($label);
                $labelAttributes = $element->getLabelAttributes();

                if (empty($labelAttributes)) {
                    $labelAttributes = $this->labelAttributes;
                }

                // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
                // labels. The semantic way is to group them inside a fieldset
                $type = $element->getAttribute('type');
                if ($type === 'multi_checkbox' || $type === 'radio') {
                    $markup = sprintf(
                        '<fieldset><legend>%s</legend>%s</fieldset>',
                        $label,
                        $elementString);
                } else {
                    if ($element->hasAttribute('id')) {
                        $labelOpen = '';
                        $labelClose = '';
                        $label = $labelHelper($element);
                    } else {
                        $labelOpen  = $labelHelper->openTag($labelAttributes);
                        $labelClose = $labelHelper->closeTag();
                    }

                    if ($label !== '' && !$element->hasAttribute('id')) {
                        $label = '<span>' . $label . '</span>';
                    }

                    // Button element is a special case, because label is always rendered inside it
                    if ($element instanceof Button) {
                        $labelOpen = $labelClose = $label = '';
                    }

                    switch ($this->labelPosition) {
                        case self::LABEL_PREPEND:
                            $markup = $labelOpen . $label . $elementString . $labelClose;
                            break;
                        case self::LABEL_APPEND:
                        default:
                            $markup = $labelOpen . $elementString . $label . $labelClose;
                            break;
                    }
                }

                if ($this->renderErrors) {
                    $markup .= $elementErrors;
                }
            } else {
                if ($this->renderErrors) {
                    $markup = $elementString . $elementErrors;
                } else {
                    $markup = $elementString;
                }
            }

            return $markup; 
        }
    }