//Zend\Form\View\Helper\FormCaptcha.php public function render(ElementInterface $element) { $captcha = $element->getCaptcha();
$helper = $captcha->getHelperName();
$helper = $renderer->plugin($helper); return $helper($element);
$pattern = '%s%s%s';
//module\Application\src\Application\View\Helper\Form\Captcha\ViewHelperCaptcha.php <?php namespace Application\View\Helper\Form\Captcha; use Zend\Form\View\Helper\Captcha\AbstractWord; use Application\View\Helper\Form\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) { // . $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__ )); } // ( 600). $captcha->setExpiration(10); // ( 10). . $captcha->setGcFreq(1); $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); } }
//module\Application\config\module.config.php ... 'view_helpers' => array( 'invokables' => array( 'viewhelpercaptcha' => 'Application\View\Helper\Form\Captcha\ViewHelperCaptcha', ), ),
//module\Application\src\Application\View\Helper\Form\Captcha\CustomCaptcha.php <?php namespace Application\View\Helper\Form\Captcha; // , . use Zend\Captcha\Image as CaptchaImage; // , . class CustomCaptcha extends CaptchaImage { protected $messageTemplates = array( self::MISSING_VALUE => ' ', self::MISSING_ID => ' ID ', self::BAD_CAPTCHA => ' ', public function getHelperName() { return 'viewhelpercaptcha'; } }
<?php namespace Album\Form; use Zend\Form\Form; use Application\Form\View\Helper\Captcha\CustomCaptcha; class AlbumForm extends Form { public function __construct($name = null) { // parent::__construct('album'); $this->setAttribute('method', 'post'); // ... // Captcha $dirdata = './data'; // CustomCaptcha $captchaImage = new CustomCaptcha(array( 'font' => $dirdata . '/fonts/arial.ttf', 'width' => 120, 'height' => 60, 'fsize' => 20, 'wordLen' => 5, 'dotNoiseLevel' => 25, 'lineNoiseLevel' => 2 )); // $captchaImage->setImgDir('public/img/captcha/'); // $captchaImage->setImgUrl('/img/captcha/'); $captchaImage->setImgAlt(' ?'); // Captcha CustomCaptcha, $this->add(array( 'type' => 'Zend\Form\Element\Captcha', 'name' => 'captcha', 'options' => array( 'captcha' => $captchaImage, ), 'attributes' => array( 'class' => 'some_class', ) )); $this->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'submit', 'value' => 'Go', 'id' => 'submitbutton', ), )); } }
echo $this->formRow($form->get('captcha')) . PHP_EOL;
Source: https://habr.com/ru/post/183232/
All Articles