zend框架2验证码助手视图呈现内联

时间:2012-11-12 12:32:26

标签: captcha zend-framework2 view-helpers

我尝试使用formCaptcha帮助渲染验证码元素查看如下:

    $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标签如何显示图像:

enter image description here

我如何将图像看作一个块,(即:图像旁边的文本字段)?

2 个答案:

答案 0 :(得分:3)

我所知道的问题没有简短的答案。 我有类似的需求,并构建了一个解决方案,并有一个博客文章和github代码可用。 [http://zendtemple.blogspot.com/2012/12/zend-framework-2-zf2-creating-view.html] [1]

我创建了自己的视图助手,并将其注册到PhpRenderer中的invokables。

然后我创建了一个新的自定义验证码图像类,它扩展了原始的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扩展原始版本,并且未使用'getHelperName'指向我新注册的视图助手'viewhelpercaptcha': \模块\应用程序\ SRC \应用程序\视图\助手\表格\定制\验证码\ 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,
), 

));

希望这有帮助。

答案 1 :(得分: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>