Add class attribute to Form Errors

后端 未结 6 2025
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 09:15

I´m developing an application using Zend Framework 2 and I use FormRow helper to render a label, the input and errors (if present) in a Form.



        
6条回答
  •  灰色年华
    2020-12-29 09:49

    See the code of formElementErrors

    Basically you could do something like:

    $this->formElementErrors($elem)
         ->setMessageOpenFormat('
  • ') ->setMessageSeparatorString('
  • ');
  • But that is quite unhandy...

    The better solution would be to extend the Zend\Form\View\Helper\FormElementErrors by your own class and then register the view-helper formElementErrors to your class. So basically you'd have something like this:

    namespace Mymodule\Form\View\Helper;
    
    use Zend\Form\View\Helper\FormElementErrors as OriginalFormElementErrors;
    
    class FormElementErrors extends OriginalFormElementErrors  
    {
        protected $messageCloseString     = '
'; protected $messageOpenFormat = '
  • '; protected $messageSeparatorString = '
  • '; }

    Last thing then would be to register the view helper with this new Class. For this you provide the following code inside your Modules Module.php

    public function getViewHelperConfig()
    {
        return array(
            'invokables' => array(
                'formelementerrors' => 'Mymodule\Form\View\Helper\FormElementErrors'
            ),
        );
    }
    

    displaimer: This code isn't tested, let me know if there are some errors, but i think this should work out quite well.

  • 提交回复
    热议问题