Skip to content
Browse files

* Make getRawValue() public

 * Fix Container::getRawValue() to actually return unfiltered values of child elements

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_QuickForm2/branches/filters_api@306084 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent bc4774c commit cd4efe0b5c17e6c98c32269f6b91aaa161d1ced1 @sad-spirit sad-spirit committed Dec 8, 2010
View
41 HTML/QuickForm2/Container.php
@@ -104,19 +104,17 @@ protected function prependsName()
}
/**
- * Returns the element's value
- *
- * The default implementation for Containers is to return an array with
- * contained elements' values. The array is indexed the same way $_GET and
- * $_POST arrays would be for these elements.
+ * Returns the array containing child elements' values
*
+ * @param bool Whether child elements should apply filters on values
* @return array|null
*/
- protected function getRawValue()
+ protected function getChildValues($filtered = false)
{
+ $method = $filtered? 'getValue': 'getRawValue';
$values = array();
foreach ($this as $child) {
- $value = $child->getValue();
+ $value = $child->$method();
if (null !== $value) {
if ($child instanceof HTML_QuickForm2_Container
&& !$child->prependsName()
@@ -145,6 +143,35 @@ protected function getRawValue()
}
/**
+ * Returns the container's value without filters applied
+ *
+ * The default implementation for Containers is to return an array with
+ * contained elements' values. The array is indexed the same way $_GET and
+ * $_POST arrays would be for these elements.
+ *
+ * @return array|null
+ */
+ public function getRawValue()
+ {
+ return $this->getChildValues(false);
+ }
+
+ /**
+ * Returns the container's value, possibly with filters applied
+ *
+ * The default implementation for Containers is to return an array with
+ * contained elements' values. The array is indexed the same way $_GET and
+ * $_POST arrays would be for these elements.
+ *
+ * @return array|null
+ */
+ public function getValue()
+ {
+ $value = $this->getChildValues(true);
+ return is_null($value)? null: $this->applyFilters($value);
+ }
+
+ /**
* Merges two arrays
*
* Merges two arrays like the PHP function array_merge_recursive does,
View
4 HTML/QuickForm2/Container/Group.php
@@ -85,9 +85,9 @@ protected function prependsName()
return strlen($this->name) > 0;
}
- protected function getRawValue()
+ protected function getChildValues($filtered = false)
{
- $value = parent::getRawValue();
+ $value = parent::getChildValues($filtered);
if (!$this->prependsName()) {
return $value;
View
2 HTML/QuickForm2/Element/Button.php
@@ -128,7 +128,7 @@ public function setValue($value)
*
* @return string|null
*/
- protected function getRawValue()
+ public function getRawValue()
{
if ((empty($this->attributes['type']) || 'submit' == $this->attributes['type']) &&
!$this->getAttribute('disabled'))
View
2 HTML/QuickForm2/Element/Input.php
@@ -86,7 +86,7 @@ public function setValue($value)
return $this;
}
- protected function getRawValue()
+ public function getRawValue()
{
return $this->getAttribute('disabled')? null: $this->getAttribute('value');
}
View
2 HTML/QuickForm2/Element/InputButton.php
@@ -91,7 +91,7 @@ public function setValue($value)
*
* return string|null
*/
- protected function getRawValue()
+ public function getRawValue()
{
return null;
}
View
2 HTML/QuickForm2/Element/InputCheckable.php
@@ -137,7 +137,7 @@ public function setValue($value)
}
}
- protected function getRawValue()
+ public function getRawValue()
{
if (!empty($this->attributes['checked']) && empty($this->attributes['disabled'])) {
return $this->getAttribute('value');
View
2 HTML/QuickForm2/Element/InputFile.php
@@ -182,7 +182,7 @@ public function toggleFrozen($freeze = null)
*
* @return array|null
*/
- protected function getRawValue()
+ public function getRawValue()
{
return $this->value;
}
View
2 HTML/QuickForm2/Element/InputImage.php
@@ -100,7 +100,7 @@ public function setValue($value)
* coordinates of user click if the image was clicked,
* null otherwise
*/
- protected function getRawValue()
+ public function getRawValue()
{
return $this->getAttribute('disabled')? null: $this->coordinates;
}
View
2 HTML/QuickForm2/Element/InputReset.php
@@ -91,7 +91,7 @@ public function setValue($value)
*
* @return string|null
*/
- protected function getRawValue()
+ public function getRawValue()
{
return null;
}
View
2 HTML/QuickForm2/Element/InputSubmit.php
@@ -99,7 +99,7 @@ public function setValue($value)
*
* @return string|null
*/
- protected function getRawValue()
+ public function getRawValue()
{
return $this->getAttribute('disabled')? null: $this->submitValue;
}
View
2 HTML/QuickForm2/Element/Select.php
@@ -420,7 +420,7 @@ protected function getFrozenHtml()
* select, array of selected options' "value" attributes in
* case of multiple selects, null if no options selected
*/
- protected function getRawValue()
+ public function getRawValue()
{
if (!empty($this->attributes['disabled']) || 0 == count($this->values)
|| ($this->data['intrinsic_validation']
View
2 HTML/QuickForm2/Element/Static.php
@@ -123,7 +123,7 @@ public function setValue($value)
*
* @return null
*/
- protected function getRawValue()
+ public function getRawValue()
{
return null;
}
View
2 HTML/QuickForm2/Element/Textarea.php
@@ -78,7 +78,7 @@ public function setValue($value)
return $this;
}
- protected function getRawValue()
+ public function getRawValue()
{
return empty($this->attributes['disabled'])? $this->value: null;
}
View
2 HTML/QuickForm2/Node.php
@@ -351,7 +351,7 @@ public function setId($id = null)
*
* @return mixed
*/
- abstract protected function getRawValue();
+ abstract public function getRawValue();
/**
* Returns the element's value, possibly with filters applied
View
22 tests/QuickForm2/Container/GroupTest.php
@@ -297,6 +297,28 @@ public function testGetValue()
$this->assertEquals($valueAnon, $anon->getValue());
}
+ public function testGetRawValue()
+ {
+ $unfiltered = array(
+ 'foo' => ' foo value ',
+ 'bar' => ' BAR VALUE '
+ );
+
+ $g = new HTML_QuickForm2_Container_Group('filtered');
+ $foo = $g->addElement('text', 'foo');
+ $bar = $g->addElement('text', 'bar');
+
+ $g->setValue($unfiltered);
+ $this->assertEquals($unfiltered, $g->getRawValue());
+
+ $g->addRecursiveFilter('trim');
+ $bar->addFilter('strtolower');
+ $this->assertEquals($unfiltered, $g->getRawValue());
+
+ $g->addFilter('count');
+ $this->assertEquals($unfiltered, $g->getRawValue());
+ }
+
/**
* Checks that JS for group rules comes after js for rules on contained elements
*/
View
30 tests/QuickForm2/ContainerTest.php
@@ -80,7 +80,7 @@ class HTML_QuickForm2_ElementImpl2 extends HTML_QuickForm2_Element
public function getType() { return 'concrete'; }
public function __toString() { return ''; }
- protected function getRawValue()
+ public function getRawValue()
{
return $this->value;
}
@@ -464,6 +464,34 @@ public function testGetValue()
), $c1->getValue());
}
+ public function testGetRawValue()
+ {
+ $c = new HTML_QuickForm2_ContainerImpl('filtered');
+
+ $foo = $c->appendChild(new HTML_QuickForm2_ElementImpl2('foo'));
+ $bar = $c->appendChild(new HTML_QuickForm2_ElementImpl2('bar'));
+
+ $foo->setValue(' foo value ');
+ $bar->setValue(' BAR VALUE ');
+ $this->assertEquals(array(
+ 'foo' => ' foo value ',
+ 'bar' => ' BAR VALUE '
+ ), $c->getRawValue());
+
+ $c->addRecursiveFilter('trim');
+ $bar->addFilter('strtolower');
+ $this->assertEquals(array(
+ 'foo' => ' foo value ',
+ 'bar' => ' BAR VALUE '
+ ), $c->getRawValue());
+
+ $c->addFilter('count');
+ $this->assertEquals(array(
+ 'foo' => ' foo value ',
+ 'bar' => ' BAR VALUE '
+ ), $c->getRawValue());
+ }
+
public function testValidate()
{
$cValidate = new HTML_QuickForm2_ContainerImpl('validate');
View
2 tests/QuickForm2/ElementTest.php
@@ -71,7 +71,7 @@ class HTML_QuickForm2_ElementImpl extends HTML_QuickForm2_Element
public function getType() { return 'concrete'; }
public function __toString() { return ''; }
- protected function getRawValue()
+ public function getRawValue()
{
return $this->value;
}
View
2 tests/QuickForm2/NodeTest.php
@@ -66,7 +66,7 @@
class HTML_QuickForm2_NodeImpl extends HTML_QuickForm2_Node
{
public function getType() { return 'concrete'; }
- protected function getRawValue() { return ''; }
+ public function getRawValue() { return ''; }
public function setValue($value) { return ''; }
public function __toString() { return ''; }

0 comments on commit cd4efe0

Please sign in to comment.
Something went wrong with that request. Please try again.