我知道你的问题没有简短的答案。我有类似的需求并构建了一个解决方案,并提供了一篇博文和 github 代码。[http://zendtemple.blogspot.com/2012/12/zend-framework-2-zf2-creating-view.html][1]
我创建了自己的视图助手并将其注册到 PhpRenderer 中的可调用对象中。
然后,我创建了一个新的自定义验证码图像类,它扩展了原始的 zend 验证码图像类并覆盖了“getHelperName”函数以指向新的助手。
默认的 Zend\Form\View\Helper\Captcha\Image.php 渲染函数采用了验证码图像、输入框和 id 的模式。它看起来像这样:
$pattern = '%s%s%s';
if ($position == self::CAPTCHA_PREPEND) {
return sprintf($pattern, $captchaInput, $separator, $img);
}
return sprintf($pattern, $img, $separator, $captchaInput);
在您的自定义视图助手中,您可以创建任何您想要的模式。
这是我的自定义视图助手。我重写了模式并将元素包装在单独的 div 中: //module\Application\src\Application\View\Helper\Form\Custom\Captcha\ViewHelperCaptcha.php
namespace Application\View\Helper\Form\Custom\Captcha;
use Zend\Form\View\Helper\Captcha\AbstractWord;
//our custom captcha image extending the orginal
use Application\View\Helper\Form\Custom\Captcha\CustomCaptcha as CaptchaAdapter;
use Zend\Form\ElementInterface;
use Zend\Form\Exception;
class ViewHelperCaptcha extends AbstractWord
{
/**
* Override
*
* Render the captcha
*
* @param ElementInterface $element
* @throws Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
//we could also set the separator here to break between the inputs and the image.
//$this->setSeparator('
')
$captcha = $element->getCaptcha();
if ($captcha === null || !$captcha instanceof CaptchaAdapter) {
throw new Exception\DomainException(sprintf(
'%s requires that the element has a "captcha" attribute of type Zend\Captcha\Image; none found',
__METHOD__
));
}
$captcha->generate();
$imgAttributes = array(
'width' => $captcha->getWidth(),
'height' => $captcha->getHeight(),
'alt' => $captcha->getImgAlt(),
'src' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(),
);
$closingBracket = $this->getInlineClosingBracket();
$img = sprintf(
'<img %s%s',
$this->createAttributesString($imgAttributes),
$closingBracket
);
$position = $this->getCaptchaPosition();
$separator = $this->getSeparator();
$captchaInput = $this->renderCaptchaInputs($element);
$pattern = '<div class="captcha_image">%s</div>%s<div class="captcha_input">%s</div>'
if ($position == self::CAPTCHA_PREPEND) {
return sprintf($pattern, $captchaInput, $separator, $img);
}
return sprintf($pattern, $img, $separator, $captchaInput);
}
}
现在我需要在 invokables 中注册这个助手:
\modules\Application\config\module.config.php
'view_helpers' => array(
'invokables' => array(
'viewhelpercaptcha' =>'Application\View\Helper\Form\Custom\Captcha\ViewHelperCaptcha',
),
),
这是我的 CustomCaptcha 扩展了原始的和 orverriding 的“getHelperName”以指向我新注册的视图助手“viewhelpercaptcha”:\module\Application\src\Application\View\Helper\Form\Custom\Captcha\CustomCaptcha.php
namespace Application\View\Helper\Form\Custom\Captcha;
//The orginial that we are intending to extend from
use Zend\Captcha\Image as CaptchaImage;
//The new extend verision were we override only what we need.
class CustomCaptcha extends CaptchaImage
{
/**
* !!! Override this function to point to the new helper.
* Get helper name used to render captcha
*
* @return string
*/
public function getHelperName()
{
return 'viewhelpercaptcha';
}
}
剩下要做的就是在表单中使用它。
//Create our custom captcha class
$captchaImage = new CustomCaptcha( array(
'font' => $dirdata . '/fonts/arial.ttf',
'width' => 200,
'height' => 100,
'wordLen' => 5,
'dotNoiseLevel' => 50,
'lineNoiseLevel' => 3)
);
$captchaImage->setImgDir($dirdata.'/captcha');
$captchaImage->setImgUrl($urlcaptcha);
//Create a form element captcha adding our custom captcha
// created above.
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human',
'captcha' => $captchaImage,
),
));
希望这可以帮助。