This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Form.php
111 lines (97 loc) · 2.68 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Form\View\Helper;
use Zend\Form\FieldsetInterface;
use Zend\Form\FormInterface;
/**
* View helper for rendering Form objects
*/
class Form extends AbstractHelper
{
/**
* Attributes valid for this tag (form)
*
* @var array
*/
protected $validTagAttributes = array(
'accept-charset' => true,
'action' => true,
'autocomplete' => true,
'enctype' => true,
'method' => true,
'name' => true,
'novalidate' => true,
'target' => true,
);
/**
* Invoke as function
*
* @param null|FormInterface $form
* @return Form
*/
public function __invoke(FormInterface $form = null)
{
if (!$form) {
return $this;
}
return $this->render($form);
}
/**
* Render a form from the provided $form,
*
* @param FormInterface $form
* @return string
*/
public function render(FormInterface $form)
{
if (method_exists($form, 'prepare')) {
$form->prepare();
}
$formContent = '';
foreach ($form as $element) {
if ($element instanceof FieldsetInterface) {
$formContent.= $this->getView()->formCollection($element);
} else {
$formContent.= $this->getView()->formRow($element);
}
}
return $this->openTag($form) . $formContent . $this->closeTag();
}
/**
* Generate an opening form tag
*
* @param null|FormInterface $form
* @return string
*/
public function openTag(FormInterface $form = null)
{
$attributes = array(
'action' => '',
'method' => 'get',
);
if ($form instanceof FormInterface) {
$formAttributes = $form->getAttributes();
if (!array_key_exists('id', $formAttributes) && array_key_exists('name', $formAttributes)) {
$formAttributes['id'] = $formAttributes['name'];
}
$attributes = array_merge($attributes, $formAttributes);
}
$tag = sprintf('<form %s>', $this->createAttributesString($attributes));
return $tag;
}
/**
* Generate a closing form tag
*
* @return string
*/
public function closeTag()
{
return '</form>';
}
}