0

我正在尝试使用 formCaptcha Helper 查看验证码元素,如下所示:

    $this->formElementErrors()
        ->setMessageOpenFormat('<br><span class="error-message">')
        ->setMessageCloseString('</span>');
    echo $this->form()->openTag($form); 
    echo $this->formRow($form->get('mail')) . "<br>";
    echo $this->formRow($form->get('password')) . "<br>";
    echo $this->formRow($form->get('name')) . "<br>";
    echo $this->formCaptcha($form->get('captcha')) . "<br>";
    echo $this->formRow($form->get('register-button')); 
    echo $this->formRow($form->get('register-type'));
    echo $this->form()->closeTag();

但是验证码使用内联模式渲染所有 HTML 标签如何显示图像:

在此处输入图像描述

我如何将图像视为一个块(即:图像下方的文本字段)?

4

2 回答 2

3

我知道你的问题没有简短的答案。我有类似的需求并构建了一个解决方案,并提供了一篇博文和 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,
), 

));

希望这可以帮助。

于 2012-12-10T23:32:15.520 回答
1

要直接在视图中渲染Zend\Captcha\Image ,您可以使用这个简单的代码。

<?php
  $captchaName = 'captcha'; // This is the name as given in form declaration

  $captcha = $form->get($captchaName)->getCaptcha();
  $captcha->generate();
?>
<div>
  <?= $this->formLabel($form->get($captchaName)) ?>
  <img width="<?= $captcha->getWidth() ?>" height="<?= $captcha->getHeight() ?>" src="<?= $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(); ?>" alt="<?= $captcha->getImgAlt() ?>">
  <input type="text" name="<?= $captchaName ?>[input]">
  <input type="hidden" value="<?= $captcha->getId() ?>" name="<?= $captchaName ?>[id]">
</div>
于 2014-03-31T19:48:00.513 回答