Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Zend\Form\View\Helper\Captcha\AbstractWord input and hidden attributes #3144

Closed
wants to merge 4 commits into from

3 participants

@stefanotorresi

AbstractWord::renderCaptchaInputs() gives the same attributes to both input and hidden html tags. This may lead to the same id attribute, if set, in both tags, wich is not W3C compliant and prevents from using <label> for attribute correctly.

Since the id attribute is unlikely to be used on the hidden element, one possible solution is to pass a copy of the attributes array without the id key to renderCaptchaHidden().

@stefanotorresi

i can make a pull request as soon as the issue is acknowledged, if the proposed solution is approved.

@Maks3w
Collaborator

Please send the patch and a test case for avoid regressions.

stefanotorresi added some commits
@stefanotorresi stefanotorresi add id attribute handling
provides html 'id' attribute uniqueness via a hardcoded suffix
2f66222
@stefanotorresi stefanotorresi cs fix 89a3a9d
@Maks3w Maks3w was assigned
@weierophinney weierophinney closed this pull request from a commit
@stefanotorresi stefanotorresi fix #3144 ba083a9
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3144'
Close #3144
e81f91e
@ghost Unknown referenced this pull request from a commit
@stefanotorresi stefanotorresi fix #3144 283b636
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3144'
Close #3144
a9e4c56
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3144' into develop
Forward port #3144
0ee4930
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 3, 2012
  1. @stefanotorresi

    fix #3144

    stefanotorresi authored
  2. @stefanotorresi

    apply php-cs-fixer run

    stefanotorresi authored
Commits on Dec 10, 2012
  1. @stefanotorresi

    add id attribute handling

    stefanotorresi authored
    provides html 'id' attribute uniqueness via a hardcoded suffix
Commits on Dec 11, 2012
  1. @stefanotorresi

    cs fix

    stefanotorresi authored
This page is out of date. Refresh to see the latest.
View
19 library/Zend/Form/View/Helper/Captcha/AbstractWord.php
@@ -43,7 +43,7 @@
/**
* Set value for captchaPosition
*
- * @param mixed $captchaPosition
+ * @param mixed $captchaPosition
* @throws Exception\InvalidArgumentException
* @return self
*/
@@ -60,6 +60,7 @@ public function setCaptchaPosition($captchaPosition)
));
}
$this->captchaPosition = $captchaPosition;
+
return $this;
}
@@ -76,12 +77,13 @@ public function getCaptchaPosition()
/**
* Set separator string for captcha and inputs
*
- * @param string $separator
+ * @param string $separator
* @return AbstractWord
*/
public function setSeparator($separator)
{
$this->separator = (string) $separator;
+
return $this;
}
@@ -104,7 +106,7 @@ public function getSeparator()
*
* More specific renderers will consume this and render it.
*
- * @param ElementInterface $element
+ * @param ElementInterface $element
* @throws Exception\DomainException
* @return string
*/
@@ -155,13 +157,18 @@ public function __invoke(ElementInterface $element = null)
* Render the hidden input with the captcha identifier
*
* @param CaptchaAdapter $captcha
- * @param array $attributes
+ * @param array $attributes
* @return string
*/
protected function renderCaptchaHidden(CaptchaAdapter $captcha, array $attributes)
{
$attributes['type'] = 'hidden';
$attributes['name'] .= '[id]';
+
+ if (isset($attributes['id'])) {
+ $attributes['id'] .= '-hidden';
+ }
+
if (method_exists($captcha, 'getId')) {
$attributes['value'] = $captcha->getId();
} elseif (array_key_exists('value', $attributes)) {
@@ -175,6 +182,7 @@ protected function renderCaptchaHidden(CaptchaAdapter $captcha, array $attribute
$this->createAttributesString($attributes),
$closingBracket
);
+
return $hidden;
}
@@ -182,7 +190,7 @@ protected function renderCaptchaHidden(CaptchaAdapter $captcha, array $attribute
* Render the input for capturing the captcha value from the client
*
* @param CaptchaAdapter $captcha
- * @param array $attributes
+ * @param array $attributes
* @return string
*/
protected function renderCaptchaInput(CaptchaAdapter $captcha, array $attributes)
@@ -198,6 +206,7 @@ protected function renderCaptchaInput(CaptchaAdapter $captcha, array $attributes
$this->createAttributesString($attributes),
$closingBracket
);
+
return $input;
}
}
View
7 library/Zend/Form/View/Helper/Captcha/Image.php
@@ -24,7 +24,7 @@ class Image extends AbstractWord
/**
* Render the captcha
*
- * @param ElementInterface $element
+ * @param ElementInterface $element
* @throws Exception\DomainException
* @return string
*/
@@ -47,6 +47,11 @@ public function render(ElementInterface $element)
'alt' => $captcha->getImgAlt(),
'src' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(),
);
+
+ if ($element->hasAttribute('id')) {
+ $imgAttributes['id'] = $element->getAttribute('id') . '-image';
+ }
+
$closingBracket = $this->getInlineClosingBracket();
$img = sprintf(
'<img %s%s',
View
15 tests/ZendTest/Form/View/Helper/FormCaptchaTest.php
@@ -62,12 +62,14 @@ protected function getTmpDir()
if (null === $this->tmpDir) {
$this->tmpDir = sys_get_temp_dir();
}
+
return $this->tmpDir;
}
public function getElement()
{
$element = new CaptchaElement('foo');
+
return $element;
}
@@ -85,8 +87,11 @@ public function testPassingElementWithDumbCaptchaRendersCorrectly()
));
$element = $this->getElement();
$element->setCaptcha($captcha);
+ $element->setAttribute('id', 'foo');
$markup = $this->helper->render($element);
$this->assertContains($captcha->getLabel(), $markup);
+ $this->assertRegExp('#<[^>]*(id="' . $element->getAttribute('id') . '")[^>]*(type="text")[^>]*>#', $markup);
+ $this->assertRegExp('#<[^>]*(id="' . $element->getAttribute('id') . '-hidden")[^>]*(type="hidden")[^>]*>#', $markup);
}
public function testPassingElementWithFigletCaptchaRendersCorrectly()
@@ -96,14 +101,18 @@ public function testPassingElementWithFigletCaptchaRendersCorrectly()
));
$element = $this->getElement();
$element->setCaptcha($captcha);
+ $element->setAttribute('id', 'foo');
$markup = $this->helper->render($element);
$this->assertContains('<pre>' . $captcha->getFiglet()->render($captcha->getWord()) . '</pre>', $markup);
+ $this->assertRegExp('#<[^>]*(id="' . $element->getAttribute('id') . '")[^>]*(type="text")[^>]*>#', $markup);
+ $this->assertRegExp('#<[^>]*(id="' . $element->getAttribute('id') . '-hidden")[^>]*(type="hidden")[^>]*>#', $markup);
}
public function testPassingElementWithImageCaptchaRendersCorrectly()
{
if (!extension_loaded('gd')) {
$this->markTestSkipped('The GD extension is not available.');
+
return;
}
if (!function_exists("imagepng")) {
@@ -125,10 +134,16 @@ public function testPassingElementWithImageCaptchaRendersCorrectly()
));
$element = $this->getElement();
$element->setCaptcha($captcha);
+ $element->setAttribute('id', 'foo');
+
$markup = $this->helper->render($element);
+
$this->assertContains('<img ', $markup);
$this->assertContains($captcha->getImgUrl(), $markup);
$this->assertContains($captcha->getId(), $markup);
+ $this->assertRegExp('#<img[^>]*(id="' . $element->getAttribute('id') . '-image")[^>]*>#', $markup);
+ $this->assertRegExp('#<input[^>]*(id="' . $element->getAttribute('id') . '")[^>]*(type="text")[^>]*>#', $markup);
+ $this->assertRegExp('#<input[^>]*(id="' . $element->getAttribute('id') . '-hidden")[^>]*(type="hidden")[^>]*>#', $markup);
}
public function testPassingElementWithReCaptchaRendersCorrectly()
Something went wrong with that request. Please try again.