Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit 93c269b

Browse files
committed
Reduced duplication of code to output forms.
Subclassed the Zend\Form\View\Helper\Form class and added a markup function that contains what was previously duplicated three places.
1 parent 1960f74 commit 93c269b

File tree

5 files changed

+51
-74
lines changed

5 files changed

+51
-74
lines changed

module/AbaLookup/Module.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getViewHelperConfig()
5959
return [
6060
'invokables' => [
6161
'anchor' => 'AbaLookup\View\Helper\AnchorLink',
62+
'form' => 'AbaLookup\Form\View\Helper\Form',
6263
'scheduleHelper' => 'AbaLookup\View\Helper\ScheduleHelper',
6364
'script' => 'AbaLookup\View\Helper\Script',
6465
'stylesheet' => 'AbaLookup\View\Helper\Stylesheet',
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace AbaLookup\Form\View\Helper;
4+
5+
use
6+
Zend\Form\FormInterface,
7+
Zend\Form\View\Helper\Form as ZendFormHelper
8+
;
9+
10+
/**
11+
* View helper that generates a form
12+
*/
13+
class Form extends ZendFormHelper
14+
{
15+
/**
16+
* Returns the HTML markup for the given form
17+
*
18+
* @param FormInterface $f The form.
19+
* @return string
20+
*/
21+
public function markup(FormInterface $f)
22+
{
23+
$v = $this->getView();
24+
$m = '';
25+
$m .= $this->openTag($f);
26+
foreach ($f as $e) {
27+
$t = $e->getAttribute('type');
28+
$l = $e->getLabel();
29+
// To style checkboxes and radio buttons, the label needs to come after the element
30+
// In all other cases, it is more semantic for the label to come before the element
31+
$content = ($l && ($t == 'checkbox' || $t == 'radio')) ?
32+
('<div class="checkbox">' . $v->formElement($e) . $v->formLabel($e, $l) . '</div>') :
33+
(($l) ? ('' . $v->formLabel($e, $l) . $v->formElement($e)) : $v->formElement($e));
34+
$m .= sprintf(
35+
'<div class="row">
36+
<div class="twelve columns">
37+
%s
38+
</div>
39+
</div>',
40+
$content
41+
);
42+
}
43+
$m .= $this->closeTag();
44+
return $m;
45+
}
46+
}

module/AbaLookup/view/aba-lookup/users/login.phtml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,7 @@
1212
$this->form->setAttribute('class', 'left');
1313
$this->form->setAttribute('action', '/users/login');
1414
$this->form->setAttribute('method', 'post');
15-
echo $this->form()->openTag($this->form);
16-
foreach ($this->form as $element) {
17-
?>
18-
<div class="row">
19-
<div class="twelve columns">
20-
<?php
21-
// To style checkboxes and radio buttons, the label needs to come after the element
22-
// In all other cases, it is more semantic for the label to come before the element
23-
$type = $element->getAttribute('type');
24-
$label = $element->getLabel();
25-
if ($label && ($type == 'checkbox' || $type == 'radio')) {
26-
echo $this->formElement($element);
27-
echo $this->formLabel($element, $label);
28-
} elseif ($label) {
29-
echo $this->formLabel($element, $label);
30-
echo $this->formElement($element);
31-
} else {
32-
echo $this->formElement($element); // There is no label
33-
}
34-
?>
35-
</div>
36-
</div>
37-
<?php
38-
}
39-
echo $this->form()->closeTag();
15+
echo $this->form()->markup($this->form);
4016
?>
4117
</div>
4218
<aside class="aside four columns">

module/AbaLookup/view/aba-lookup/users/profile-edit.phtml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,10 @@
1010
$this->form->setAttribute('class', 'left');
1111
$this->form->setAttribute('action', '/users' . '/' . $this->user->getId() . '/profile/edit');
1212
$this->form->setAttribute('method', 'post');
13-
echo $this->form()->openTag($this->form);
14-
15-
foreach ($this->form as $element) {
16-
?>
17-
<div class="row">
18-
<div class="twelve columns">
19-
<?php
20-
$label = $element->getLabel();
21-
if ($label) {
22-
echo $this->formLabel($element, $label);
23-
}
24-
echo $this->formElement($element);
25-
?>
26-
</div>
27-
</div>
28-
<?php
29-
}
30-
31-
echo $this->form()->closeTag();
13+
echo $this->form()->markup($this->form);
3214
?>
3315
</div>
3416
<aside class="aside four columns">
3517
<h3>Details</h3>
36-
<p>
37-
Edit your profile information.
38-
</p>
18+
<p>Edit your profile information.</p>
3919
</aside>

module/AbaLookup/view/aba-lookup/users/register.phtml

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,6 @@
1616
$this->form->setAttribute('class', 'left');
1717
$this->form->setAttribute('action', sprintf('/users/register/%s', $this->type));
1818
$this->form->setAttribute('method', 'post');
19-
echo $this->form()->openTag($this->form);
20-
foreach ($this->form as $element) {
21-
?>
22-
<div class="row">
23-
<div class="twelve columns">
24-
<?php
25-
// To style checkboxes and radio buttons, the label needs to come after the element
26-
// In all other cases, it is more semantic for the label to come before the element
27-
$type = $element->getAttribute('type');
28-
$label = $element->getLabel();
29-
if ($label && ($type == 'checkbox' || $type == 'radio')) {
30-
echo '<div class="checkbox">';
31-
echo $this->formElement($element);
32-
echo $this->formLabel($element, $label);
33-
echo '</div>';
34-
} elseif ($label) {
35-
echo $this->formLabel($element, $label);
36-
echo $this->formElement($element);
37-
} else {
38-
echo $this->formElement($element); // There is no label
39-
}
40-
?>
41-
</div>
42-
</div>
43-
<?php
44-
}
45-
echo $this->form()->closeTag();
19+
echo $this->form()->markup($this->form);
4620
?>
4721
</div>

0 commit comments

Comments
 (0)