自定义 zend_form 验证码输出? [英] Customize zend_form Captcha output?

查看:26
本文介绍了自定义 zend_form 验证码输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 zend_form 中使用验证码.

$captcha_element = new Zend_Form_Element_Captcha('验证码',array('label' => '将字符写入字段','验证码' =>大批('验证码' =>'图片','wordLen' =>6、'超时' =>300,'字体' =>DOC_ROOT .'/data/fonts/Vera.ttf','imgDir' =>$图像目录,'imgUrl' =>$umageurl)));

这会产生:

<label for="captcha-input" class="required">将字符写入字段</label></dt><dd id="验证码元素"><img width="200" height="50" alt="" src="http://sitename.com/captcha/09dd951939c6cdf7fa28f2b7d322ea95.png"><input type="hidden" name="captcha[id]" value="09dd951939c6cdf7fa28f2b7d322ea95" id="captcha-id"><input type="text" name="captcha[input]" id="captcha-input" value=""></dd>

不过.- 我需要以下(验证码元素单独包装到一些标签中):

<label for="captcha-input" class="required">将字符写入字段</label></dt><dd id="验证码元素"><div><span><input type="text" name="captcha[input]" id="captcha-input" value=""></span></div><div><span><img width="200" height="50" alt="" src="http://sitename.com/captcha/09dd951939c6cdf7fa28f2b7d322ea95.png"><input type="hidden" name="captcha[id]" value="09dd951939c6cdf7fa28f2b7d322ea95" id="captcha-id"></span></div></dd>

我不知道该怎么做.我可以通过使用一些自定义装饰器来实现这一点吗?或者涉及自定义验证码?

解决方案

这有点棘手,但我准备了一个自定义 Captcha 元素.我还需要准备自定义验证码装饰器.在这两种情况下,我都需要覆盖 Zend_Form_Element_CaptchaZend_Form_Decorator_Captcha 中的默认渲染方法.我还取消了 Zend_Form_Decorator_Captcha_Word,因为我将其功能直接合并到 My_Form_Decorator_Captcha 中.这有两个原因.第一个是表单元素的顺序发生了变化,即从默认的img、input hidden、input text变成了input text、img、input hidden.第二个原因是需要添加div和span标签.

希望它们会有所帮助:

My_Form_Element_Captcha:

class My_Form_Element_Captcha 扩展 Zend_Form_Element_Captcha {公共函数渲染(Zend_View_Interface $view = null){$captcha = $this->getCaptcha();$captcha->setName($this->getFullyQualifiedName());$decorators = $this->getDecorators();//下面是使用新装饰器的地方$decorator = new My_Form_Decorator_Captcha(array('captcha' => $captcha));array_unshift($decorators, $decorator);$decorator = $captcha->getDecorator();$this->setDecorators($decorators);$this->setValue($this->getCaptcha()->generate());返回 Zend_Form_Element::render($view);}}

My_Form_Decorator_Captcha:

class My_Form_Decorator_Captcha 扩展 Zend_Form_Decorator_Captcha {公共函数渲染($content){$element = $this->getElement();if (!method_exists($element, 'getCaptcha')) {返回 $content;}$view = $element->getView();if (null === $view) {返回 $content;}$name = $element->getFullyQualifiedName();$hiddenName = $name .'[ID]';$textName = $name .'[输入]';$label = $element->getDecorator("Label");如果($标签){$label->setOption("id", $element->getId() ."-input");}$placement = $this->getPlacement();$separator = $this->getSeparator();$captcha = $element->getCaptcha();$markup = $captcha->render($view, $element);$hidden = $view->formHidden($hiddenName, $element->getValue(), $element->getAttribs());$text = $view->formText($textName, '', $element->getAttribs());//改变元素的顺序并添加 div 和 span 标签.开关($放置){案例准备":$content = '

'.$文本.'</div></span>'.'<div><span>'.$标记.$隐藏.'</div></span>'.$分隔符.$内容;休息;案例附加":默认:$content = $content .$分隔符.'<div><span>'.$文本.'</div></span>'.'<div><span>'.$标记.$隐藏.'</div></span>';}返回 $content;}}

I'm using captcha in my zend_form.

$captcha_element = new Zend_Form_Element_Captcha(
    'captcha',
    array('label' => 'Write the chars to the field',
        'captcha' => array(
            'captcha' => 'Image',
            'wordLen' => 6,
            'timeout' => 300,
            'font' => DOC_ROOT . '/data/fonts/Vera.ttf',
            'imgDir' => $imagedir,
            'imgUrl' => $umageurl
        )
    )
);

This generates:

<dt id="captcha-input-label">
    <label for="captcha-input" class="required">Write the chars to the field</label>
</dt>

<dd id="captcha-element">
    <img width="200" height="50" alt="" src="http://sitename.com/captcha/09dd951939c6cdf7fa28f2b7d322ea95.png">
    <input type="hidden" name="captcha[id]" value="09dd951939c6cdf7fa28f2b7d322ea95" id="captcha-id">
    <input type="text" name="captcha[input]" id="captcha-input" value="">
</dd>

However. - I need following instead (captcha elements are wrapped into some tags individually):

<dt id="captcha-input-label">
    <label for="captcha-input" class="required">Write the chars to the field</label>
</dt>

<dd id="captcha-element">
    <div><span>
        <input type="text" name="captcha[input]" id="captcha-input" value="">
    </span></div>
    <div><span>
        <img width="200" height="50" alt="" src="http://sitename.com/captcha/09dd951939c6cdf7fa28f2b7d322ea95.png">
        <input type="hidden" name="captcha[id]" value="09dd951939c6cdf7fa28f2b7d322ea95" id="captcha-id">
    </span></div>
</dd>

I can't figure out how would I do this. Can I accomplish this by using some custom decorators? or woud that involve custom captcha ?

解决方案

It was a bit tricky, but I prepared a custom Captcha element. I also needed to prepare custom Captcha decorator. In both cases I needed to override default render methods in both Zend_Form_Element_Captcha and Zend_Form_Decorator_Captcha. I also eliminated Zend_Form_Decorator_Captcha_Word since I incorporated its functionality directly into My_Form_Decorator_Captcha. There were two reasons for this. The first one is that order of form elements was changed, i.e. from default img, input hidden, input text into input text, img, input hidden. The second reason is that div and span tags needed to be added.

Hopefully, they will be helpful:

My_Form_Element_Captcha:

class My_Form_Element_Captcha extends Zend_Form_Element_Captcha {

    public function render(Zend_View_Interface $view = null)     {
        $captcha    = $this->getCaptcha();
        $captcha->setName($this->getFullyQualifiedName());

        $decorators = $this->getDecorators();

        // BELOW IS WHERE THE NEW DECORATOR IS USED

        $decorator = new My_Form_Decorator_Captcha(array('captcha' => $captcha));

        array_unshift($decorators, $decorator);

        $decorator  = $captcha->getDecorator();

        $this->setDecorators($decorators);


        $this->setValue($this->getCaptcha()->generate());

        return Zend_Form_Element::render($view);
    }
}

My_Form_Decorator_Captcha:

class My_Form_Decorator_Captcha extends Zend_Form_Decorator_Captcha {

     public function render($content) {
        $element = $this->getElement();
        if (!method_exists($element, 'getCaptcha')) {
            return $content;
        }

        $view = $element->getView();
        if (null === $view) {
            return $content;
        }


        $name = $element->getFullyQualifiedName();

        $hiddenName = $name . '[id]';
        $textName = $name . '[input]';

        $label = $element->getDecorator("Label");
        if ($label) {
            $label->setOption("id", $element->getId() . "-input");
        }

        $placement = $this->getPlacement();
        $separator = $this->getSeparator();

        $captcha = $element->getCaptcha();
        $markup = $captcha->render($view, $element);
        $hidden = $view->formHidden($hiddenName, $element->getValue(), $element->getAttribs());
        $text = $view->formText($textName, '', $element->getAttribs());


        // CHANGE THE ORDER OF ELEMENTS AND ADD THE div AND span TAGS.

        switch ($placement) {
            case 'PREPEND':
                $content = '<div><span>' . $text . '</div></span>' .
                        '<div><span>' . $markup . $hidden . '</div></span>' .
                        $separator . $content;
                break;
            case 'APPEND':
            default:
                $content = $content . $separator .
                        '<div><span>' . $text . '</div></span>' .
                        '<div><span>' . $markup . $hidden . '</div></span>';
        }

        return $content;
    }

}

这篇关于自定义 zend_form 验证码输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆