开发者

Zend Error String

I have a code here that calles my Errors in group perfectly, but i wanted to add something.

$this->setDecorators(array(
            'FormErrors',
     开发者_高级运维       'FormElements',
        array('HtmlTag',array('tag' => 'dl','class' => 'zend_form')),
            'Form'
           ));

everytime this error gets called. I want to just add a text above the dl or below it..thats says

"ARLET ALERT ERROR ERROR" just one simple string everytime Zend Errors get called

Any ideas?


You could create a custom decorator.

App_Form_Decorator_AdditionalError
    extends Zend_Form_Decorator_Abstract
{
    protected $_options = array(
        'additionalError' => 'ERROR'
    );

    public function render( $content )
    {
        $element = $this->getElement();
        if( $element->isErrors() )
        {
            $element->addError( $this->getOption( 'additionalError' ) );
        }

        return $content;
    }
}

But this would only push an additional error to the stack of errors. You could get more fancy by actually prepending or appending the error to the $content in stead of simply adding it with addError(). (And perhaps extend the HtmlTag decorator in stead of the Abstract decorator). But I'm to lazy to make complete example of that right now. Sorry. Maybe I'll get back to this tomorrow. Hope this helps/inspires you nonetheless.

BTW; usage of the above would be:

$this->setDecorators(array(
            array(
                'AdditionalError',
                array( 'additionalError' => 'Some additional error message' )
            )
            'FormErrors',
            // etc.

Edit:
Alright, so I had a good nights sleep, and here is a better suggestion. It extends Desciption to take advantage of already defined options, like tag, escape etc. It's even translatable (yay!! :) ). And this one adds a setError option and overrides the render method. It is untested though, so you might get some bugs, and/or syntax errors.

class App_Form_Decorator_AdditionalError
    extends Zend_Form_Decorator_Description
{
    protected $_error = 'error';

    public function setError( $error )
    {
        $this->_error = (string) $error;
        return $this;
    }

    public function getError()
    {
        if( null !== ( $error = $this->getOption( 'error' ) ) )
        {
            $this->setError( $error );
            $this->removeOption( 'error' );
        }

        return $this->_error;
    }

    public function render( $content )
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if( !$element->isErrors() || null === $view )
        {
            return $content;
        }

        $error = $this->getError();
        $error = trim( $error );

        if( !empty( $error ) && ( null !== ( $translator = $element->getTranslator() ) ) )
        {
            $error = $translator->translate( $error );
        }

        if( empty( $error ) )
        {
            return $content;
        }

        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $tag       = $this->getTag();
        $class     = $this->getClass();
        $escape    = $this->getEscape();

        $options   = $this->getOptions();

        if( $escape )
        {
            $error = $view->escape( $error );
        }

        if( !empty( $tag ) )
        {
            require_once 'Zend/Form/Decorator/HtmlTag.php';
            $options[ 'tag' ] = $tag;
            $decorator = new Zend_Form_Decorator_HtmlTag( $options );
            $error = $decorator->render( $error );
        }

        switch( $placement )
        {
            case self::PREPEND:
                return $error . $separator . $content;
            case self::APPEND:
            default:
                return $content . $separator . $error;
        }
    }
}

Usage:

public function init()
{
    $this->addPrefixPath( 'App_Form', 'App/Form' ); // or any other namespace you will use
    $this->setDecorators( array(
        'FormErrors',
        'FormElements',
        array(
            'AdditionalError',
            array( 'error' => 'You messed up! :)', 'placement' => 'prepend', 'tag' => 'div', 'class' => 'additional-error' )
        ),
        array(
            'HtmlTag',
            array( 'tag' => 'dl', 'class' => 'zend_form' )
        ),
        'Form'
    ) );
}

Edit 2:
I've tested it now, and removed syntax errors and bugs. Seems to be working as expected now.


In the past, I have used the Errors decorator but applied to the form itself (rather than the typical use on a form element).

In the form:

$this->setDecorators(
    array(
        'FormElements',
        // whatever other wrapping you need
        // ...
        'Form',
        array('Errors', array('placement' => 'prepend')),

    )
);

In the controller:

public function someAction()
{
    $form = new My_Form();
    if ($this->getRequest()->isPost()) {
        if ($form->isValid($this->getRequest()->getPost())) {
            // Perform processing on the submitted data
        } else {
            $form->setErrors(array('Note errors below'));
        }
    }
    $this->view->form = $form;
}

In the view

<?= $this->form ?>

One could compellingly argue that the controller should not be specifying error message text. In this sense, a custom decorator - as suggested by @fireeyedboy - that detects the mere presence of an error in any of the form elements and displays a fixed (or a constructor-specified) error message is probably the cleaner solution. But the approach above has worked for me as a quick-though-slightly-dirty solution.

Hope it helps!


If you want your string to be appended to all errors in ZF, you can use CSS2 :before pseudoselector with content property. Just target ZF's errors element, which AFAIR is ul.errors:

ul.errors:before {
    content: 'Look, there are errors below!';
}

You can also target some errors, if you pass correct option to decorator and refer to them by class or id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜