3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Form\View\Helper
;
12 use Zend\Form\ElementInterface
;
13 use Zend\Form\Exception
;
15 class FormButton
extends FormInput
18 * Attributes valid for the button tag
22 protected $validTagAttributes = array(
28 'formenctype' => true,
30 'formnovalidate' => true,
37 * Valid values for the button type
41 protected $validTypes = array(
48 * Invoke helper as functor
50 * Proxies to {@link render()}.
52 * @param ElementInterface|null $element
53 * @param null|string $buttonContent
54 * @return string|FormButton
56 public function __invoke(ElementInterface
$element = null, $buttonContent = null)
62 return $this->render($element, $buttonContent);
66 * Render a form <button> element from the provided $element,
67 * using content from $buttonContent or the element's "label" attribute
69 * @param ElementInterface $element
70 * @param null|string $buttonContent
71 * @throws Exception\DomainException
74 public function render(ElementInterface
$element, $buttonContent = null)
76 $openTag = $this->openTag($element);
78 if (null === $buttonContent) {
79 $buttonContent = $element->getLabel();
80 if (null === $buttonContent) {
81 throw new Exception\
DomainException(sprintf(
82 '%s expects either button content as the second argument, ' .
83 'or that the element provided has a label value; neither found',
88 if (null !== ($translator = $this->getTranslator())) {
89 $buttonContent = $translator->translate(
90 $buttonContent, $this->getTranslatorTextDomain()
95 $escape = $this->getEscapeHtmlHelper();
97 return $openTag . $escape($buttonContent) . $this->closeTag();
101 * Generate an opening button tag
103 * @param null|array|ElementInterface $attributesOrElement
104 * @throws Exception\InvalidArgumentException
105 * @throws Exception\DomainException
108 public function openTag($attributesOrElement = null)
110 if (null === $attributesOrElement) {
114 if (is_array($attributesOrElement)) {
115 $attributes = $this->createAttributesString($attributesOrElement);
116 return sprintf('<button %s>', $attributes);
119 if (!$attributesOrElement instanceof ElementInterface
) {
120 throw new Exception\
InvalidArgumentException(sprintf(
121 '%s expects an array or Zend\Form\ElementInterface instance; received "%s"',
123 (is_object($attributesOrElement) ?
get_class($attributesOrElement) : gettype($attributesOrElement))
127 $element = $attributesOrElement;
128 $name = $element->getName();
129 if (empty($name) && $name !== 0) {
130 throw new Exception\
DomainException(sprintf(
131 '%s requires that the element has an assigned name; none discovered',
136 $attributes = $element->getAttributes();
137 $attributes['name'] = $name;
138 $attributes['type'] = $this->getType($element);
139 $attributes['value'] = $element->getValue();
143 $this->createAttributesString($attributes)
148 * Return a closing button tag
152 public function closeTag()
158 * Determine button type to use
160 * @param ElementInterface $element
163 protected function getType(ElementInterface
$element)
165 $type = $element->getAttribute('type');
170 $type = strtolower($type);
171 if (!isset($this->validTypes
[$type])) {