加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework – 自定义zend_form验证码输出?

发布时间:2020-12-13 14:07:29 所属栏目:PHP教程 来源:网络整理
导读:我在我的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
我在我的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
        )
    )
);

这产生:

<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>

然而. – 我需要以下代码(captcha元素单独包装在一些标签中):

<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>

我不知道我该怎么做我可以通过使用一些自定义装饰来完成这个吗?或涉及自定义验证码?

这有点棘手,但我准备了一个自定义的Captcha元素.我还需要准备自定义的Captcha装饰器.在这两种情况下,我需要覆盖Zend_Form_Element_Captcha和Zend_Form_Decorator_Captcha中的默认渲染方法.我也删除了Zend_Form_Decorator_Captcha_Word,因为我将其功能直接并入My_Form_Decorator_Captcha.这有两个原因.第一个是表单元素的顺序被改变,即从默认的img,输入隐藏,输入文本到输入文本,img,输入隐藏.第二个原因是需要添加div和span标签.

希望他们会有所帮助:

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;
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读