2012-11-12 97 views
0

我嘗試使用formCaptcha助手查看如何遵循呈現驗證碼元素:Zend框架2的驗證碼助手視圖渲染直列

$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

我怎樣才能看到圖像作爲一個塊,(即:圖像下方的文本字段)?

回答

3

沒有簡短的回答你的問題,我知道的。 我有類似的需求,並構建了一個解決方案,並有一個博客文章加github代碼可用。 [http://zendtemple.blogspot.com/2012/12/zend-framework-2-zf2-creating-view.html][1]

我創建了我自己的視圖幫助器並將其註冊到可調用的PhpRenderer。

我然後創建一個擴展原始Zend的驗證碼圖像類,並覆蓋了「getHelperName」功能在新的幫手來點新的自定義驗證碼圖片類。

默認的Zend \表格\查看\助手\驗證碼\ 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擴展原始和我的新註冊的視圖助手「viewhelpercaptcha」 orverriding的「getHelperName」到一點: \模塊\應用程序的\ src \應用\查看\助手\ 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, 
), 

));

希望這會有所幫助。

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

這幫了我很多時間來自定義模板中的元素。謝謝 – vandersondf