ZF2在表单验证错误上呈现自定义类

时间:2016-04-11 22:52:27

标签: php zend-framework2 zend-form2

如何在表单行周围包装fieldset标记,如何在元素无法验证时向fieldset添加has-danger类?

我认为我需要创建一个viewhelper,检查一个特定的formelement是否有一个验证消息并用行集包装行,但我不确定这是否是正确的方法或怎么做它

我想要的验证错误输出:

<fieldset class="has-danger"><label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control 
input-error" value="f"><ul><li>Needs to be    5-20 characters long</li>
</ul></fieldset>

register.phtml:

<?php
$form = $this->registerForm;
$form->prepare();
echo $this->form()->openTag($form);

echo $this->formHidden($form->get('secret'));
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('confirm-email'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag($form);

1 个答案:

答案 0 :(得分:0)

好的,您应该使用formLabel()formElement()formElementErrors()视图助手而不是formRow()

根据我的ZF2知识,formRow()视图帮助程序不会让您轻松控制HTML输出。它在一行代码中显示完整的输入HTML(标签+输入+错误)。

要简单地实现目标,您的一个输入的phtml结构必须如下:

// NAME_OF_YOUR_FILE.phtml
<?php
    $userNameInput       = $form->get('username');
    $userNameInputErrors = $this->formElementErrors($userNameInput);
?>
<fieldset class="<?= $userNameInputErrors ? 'has-danger' : '' ?>" >
    <?= $this->formLabel($userNameInput) ?>
    <?= $this->formElement($userNameInput) ?>
    <?= $userNameInputErrors ?>
</fieldset>

对于表单中的每个字段,这是一项艰苦的工作,因此我建议您将此逻辑放在视图助手中。

// MyFormRow.php 
<?php
    namespace Your\Namespace;

    use Zend\View\Helper\AbstractHelper;

    class MyFormRow extends AbstractHelper
    {
        protected $formLabelViewHelper;
        protected $formElementViewHelper;
        protected $formElementErrorsViewHelper;

        public function __construct($formLabelViewHelper, $formElementViewHelper, $formElementErrorsViewHelper)
        {
            $this->formLabelViewHelper         = $formLabelViewHelper;
            $this->formElementViewHelper       = $formElementViewHelper;
            $this->formElementErrorsViewHelper = $formElementErrorsViewHelper;
        }

        public function __invoke($formElement)
        {
             $html = '';

             $errors = $this->formElementErrorsViewHelper->__invoke($formElement);     

             $html .= '<fieldset class=" . ($errors ? 'has-danger' : '') . ">';    
                 $html .= $this->formLabelViewHelper->__invoke($formElement); 
                 $html .= $this->formElementViewHelper->__invoke($formElement);  
                 $html .= $errors;   
             $html .= '</fieldset>';

             return $html;
        }
    }
?>

Module.php

中声明
// In your Module.php
<?php

    ...

    public function getViewHelperConfig()
    {
        return [
            'factories'  => [
                'myFormRow' => function (HelperPluginManager $helperPluginManager) {
                    $formLabelViewHelper        = $helperPluginManager->get('formLabel');
                    $formElementViewHelper      = $helperPluginManager->get('formElement');
                    $formElementErrorsViewHelper = $helperPluginManager->get('formElementErrors');

                    return new MyFormRow($formLabelViewHelper, $formElementViewHelper, $formElementErrorsViewHelper);
                },
            ],
        ];
    }
?>

现在在你的phtml文件中,只需:

// NAME_OF_YOUR_FILE.phtml

...

<?= echo $this->myFormRow($form->get('username')) ?>

...