Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Element values
Browse files Browse the repository at this point in the history
Changeset created for @waltertamboer on his suggestions

Element values may or may not be appropriate string values. As an
example, you may want to seed these as DateTime objects or other such
value objects -- which means that when it comes time to render them,
you may run into issues.

As such, I've added "getValue()" and "setValue()" to the
ElementInterface, and made Element proxy setAttribute('value', $value)
to set the element value. View helpers now will pull the value from the
element and add it to the attributes if it should be rendered.

This simplifies the CSRF element, as it can simply override getValue()
at this point. The DateTime element (and those deriving from it) can now
cast a DateTime object using a format (defaulting to RFC-3339, which is
what HTML5 expects). Checkboxes and radios now pull the value, not the
value attribute.
  • Loading branch information
weierophinney committed Jul 25, 2012
1 parent 812dd99 commit 437ff0b
Show file tree
Hide file tree
Showing 34 changed files with 347 additions and 95 deletions.
31 changes: 31 additions & 0 deletions library/Zend/Form/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Element implements ElementInterface
*/
protected $options = array();

/**
* @var mixed
*/
protected $value;

/**
* @param null|int|string $name Optional name for the element
Expand Down Expand Up @@ -149,6 +153,11 @@ public function getOption($option)
*/
public function setAttribute($key, $value)
{
// Do not include the value in the list of attributes
if ($key === 'value') {
$this->setValue($value);
return $this;
}
$this->attributes[$key] = $value;
return $this;
}
Expand Down Expand Up @@ -223,6 +232,28 @@ public function clearAttributes()
return $this;
}

/**
* Set the element value
*
* @param mixed $value
* @return Element
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}

/**
* Retrieve the element value
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}

/**
* Set the label used for this element
*
Expand Down
34 changes: 5 additions & 29 deletions library/Zend/Form/Element/Csrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,14 @@ protected function getValidator()
}

/**
* Override: set a single element attribute
* Retrieve value
*
* Does not allow setting value attribute; this will always be
* retrieved from the validator.
*
* @param string $name
* @param mixed $value
* @return Csrf
*/
public function setAttribute($name, $value)
{
if ('value' == $name) {
// Do not allow setting this
return;
}
return parent::setAttribute($name, $value);
}

/**
* Override: retrieve a single element attribute
*
* Retrieves validator hash when asked for 'value' attribute;
* otherwise, proxies to parent.
*
* @param string $name
* @return mixed
* Retrieves the hash from the validator
*
* @return void
*/
public function getAttribute($name)
public function getValue()
{
if ($name != 'value') {
return parent::getAttribute($name);
}
$validator = $this->getValidator();
return $validator->getHash();
}
Expand Down
53 changes: 53 additions & 0 deletions library/Zend/Form/Element/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,64 @@ class DateTime extends Element implements InputProviderInterface
'type' => 'datetime',
);

/**
* Date format to use for DateTime values. By default, this is RFC-3339,
* which is what HTML5 dictates.
*
* @var string
*/
protected $format = PhpDateTime::RFC3339;

/**
* @var array
*/
protected $validators;

/**
* Retrieve the element value
*
* If the value is a DateTime object, and $returnFormattedValue is true
* (the default), we return the string
* representation using the currently registered format.
*
* If $returnFormattedValue is false, the original value will be
* returned, regardless of type.
*
* @param bool $returnFormattedValue
* @return mixed
*/
public function getValue($returnFormattedValue = true)
{
$value = parent::getValue();
if (!$value instanceof PhpDateTime || !$returnFormattedValue) {
return $value;
}
$format = $this->getFormat();
return $value->format($format);
}

/**
* Set value for format
*
* @param string format
* @return DateTime
*/
public function setFormat($format)
{
$this->format = (string) $format;
return $this;
}

/**
* Retrieve the DateTime format to use for the value
*
* @return string
*/
public function getFormat()
{
return $this->format;
}

/**
* Get validators
*
Expand Down
15 changes: 15 additions & 0 deletions library/Zend/Form/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public function setAttributes($arrayOrTraversable);
*/
public function getAttributes();

/**
* Set the value of the element
*
* @param mixed $value
* @return ElementInterface
*/
public function setValue($value);

/**
* Retrieve the element value
*
* @return mixed
*/
public function getValue();

/**
* Set the label (if any) used for this element
*
Expand Down
7 changes: 4 additions & 3 deletions library/Zend/Form/View/Helper/FormButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ public function openTag($attributesOrElement = null)
));
}

$attributes = $element->getAttributes();
$attributes['name'] = $name;
$attributes['type'] = $this->getType($element);
$attributes = $element->getAttributes();
$attributes['name'] = $name;
$attributes['type'] = $this->getType($element);
$attributes['value'] = $element->getValue();

return sprintf(
'<button %s>',
Expand Down
3 changes: 2 additions & 1 deletion library/Zend/Form/View/Helper/FormCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function render(ElementInterface $element)
$attributes['type'] = $this->getInputType();
$closingBracket = $this->getInlineClosingBracket();

if (isset($attributes['value']) && $attributes['value'] == $element->getCheckedValue()) {
$value = $element->getValue();
if ($value === $element->getCheckedValue()) {
$attributes['checked'] = 'checked';
}
$attributes['value'] = $element->getCheckedValue();
Expand Down
7 changes: 4 additions & 3 deletions library/Zend/Form/View/Helper/FormInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ public function render(ElementInterface $element)
));
}

$attributes = $element->getAttributes();
$attributes['name'] = $name;
$attributes['type'] = $this->getType($element);
$attributes = $element->getAttributes();
$attributes['name'] = $name;
$attributes['type'] = $this->getType($element);
$attributes['value'] = $element->getValue();

return sprintf(
'<input %s%s',
Expand Down
7 changes: 1 addition & 6 deletions library/Zend/Form/View/Helper/FormMultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,7 @@ public function render(ElementInterface $element)

$attributes['name'] = $name;
$attributes['type'] = $this->getInputType();

$selectedOptions = array();
if (isset($attributes['value'])) {
$selectedOptions = (array) $attributes['value'];
unset($attributes['value']);
}
$selectedOptions = (array) $element->getValue();

$rendered = $this->renderOptions($element, $options, $selectedOptions, $attributes);

Expand Down
11 changes: 5 additions & 6 deletions library/Zend/Form/View/Helper/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ public function render(ElementInterface $element)
$options = (array) $attributes['options'];
unset($attributes['options']);

$value = array();
if (isset($attributes['value'])) {
$value = $attributes['value'];
$value = $this->validateMultiValue($value, $attributes);
unset($attributes['value']);
};
$value = $this->validateMultiValue($element->getValue(), $attributes);

$attributes['name'] = $name;
if (array_key_exists('multiple', $attributes) && $attributes['multiple']) {
Expand Down Expand Up @@ -245,6 +240,10 @@ public function __invoke(ElementInterface $element = null)
*/
protected function validateMultiValue($value, array $attributes)
{
if (null === $value) {
return array();
}

if (!is_array($value)) {
return (array) $value;
}
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Form/View/Helper/FormTextarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function render(ElementInterface $element)

$attributes = $element->getAttributes();
$attributes['name'] = $name;
$content = (string) $element->getAttribute('value');
$content = (string) $element->getValue();
$escapeHtml = $this->getEscapeHtmlHelper();

return sprintf(
Expand Down
32 changes: 32 additions & 0 deletions tests/Zend/Form/Element/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace ZendTest\Form\Element;

use DateTime;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Form\Element\DateTime as DateTimeElement;
use Zend\Form\Factory;
Expand Down Expand Up @@ -57,4 +58,35 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri
}
}
}

public function testUsesRfc3339FormatByDefault()
{
$element = new DateTimeElement('foo');
$this->assertEquals(DateTime::RFC3339, $element->getFormat());
}

public function testSpecifyingADateTimeValueWillReturnRfc3339FormattedStringByDefault()
{
$date = new DateTime();
$element = new DateTimeElement('foo');
$element->setValue($date);
$this->assertEquals($date->format($date::RFC3339), $element->getValue());
}

public function testValueIsFormattedAccordingToFormatInElement()
{
$date = new DateTime();
$element = new DateTimeElement('foo');
$element->setFormat($date::RFC2822);
$element->setValue($date);
$this->assertEquals($date->format($date::RFC2822), $element->getValue());
}

public function testCanRetrieveDateTimeObjectByPassingBooleanFalseToGetValue()
{
$date = new DateTime();
$element = new DateTimeElement('foo');
$element->setValue($date);
$this->assertSame($date, $element->getValue(false));
}
}
11 changes: 9 additions & 2 deletions tests/Zend/Form/View/Helper/FormButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public function getCompleteElement()
'size' => 'value',
'src' => 'value',
'step' => 'value',
'value' => 'value',
'width' => 'value',
));
$element->setValue('value');
return $element;
}

Expand All @@ -192,7 +192,14 @@ public function testAllValidFormMarkupAttributesPresentInElementAreRendered($att
$element = $this->getCompleteElement();
$element->setAttribute('label', '{button_content}');
$markup = $this->helper->render($element);
$expect = sprintf('%s="%s"', $attribute, $element->getAttribute($attribute));
switch ($attribute) {
case 'value':
$expect = sprintf('%s="%s"', $attribute, $element->getValue());
break;
default:
$expect = sprintf('%s="%s"', $attribute, $element->getAttribute($attribute));
break;
}
$this->$assertion($expect, $markup);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Zend/Form/View/Helper/FormCheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function testUsesOptionsAttributeToGenerateCheckedAndUnCheckedValues()
public function testUsesElementValueToDetermineCheckboxStatus()
{
$element = $this->getElement();
$element->setAttribute('value', 'checked');
$element->setValue('checked');
$markup = $this->helper->render($element);

$this->assertRegexp('#value="checked"\s+checked="checked"#', $markup);
$this->assertNotRegexp('#value="unchecked"\s+checked="checked"#', $markup);
$this->assertRegexp('#checked="checked"\s+value="checked"#', $markup);
$this->assertNotRegexp('#checked="checked"\s+value="unchecked"#', $markup);
}

public function testNoOptionsAttributeCreatesDefaultCheckedAndUncheckedValues()
Expand Down
11 changes: 9 additions & 2 deletions tests/Zend/Form/View/Helper/FormColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public function getCompleteElement()
'size' => 'value',
'src' => 'value',
'step' => 'value',
'value' => 'value',
'width' => 'value',
));
$element->setValue('value');
return $element;
}

Expand All @@ -130,7 +130,14 @@ public function testAllValidFormMarkupAttributesPresentInElementAreRendered($att
{
$element = $this->getCompleteElement();
$markup = $this->helper->render($element);
$expect = sprintf('%s="%s"', $attribute, $element->getAttribute($attribute));
switch ($attribute) {
case 'value':
$expect = sprintf('%s="%s"', $attribute, $element->getValue());
break;
default:
$expect = sprintf('%s="%s"', $attribute, $element->getAttribute($attribute));
break;
}
$this->$assertion($expect, $markup);
}

Expand Down
Loading

0 comments on commit 437ff0b

Please sign in to comment.