* Add KeyDescender::getSilent() to get a key without an exception.
* Add KeyDescender::getSilent() to get a key without an exception.
* Fix a bug in ValidateFormKeyEvent where we weren't using "p." to prefix the |post_variable|.
diff --git a/base/key_descender.php b/base/key_descender.php
index 3ce4e70..6875df1 100644
--- a/base/key_descender.php
+++ b/base/key_descender.php
@@ -66,6 +66,20 @@ class KeyDescender
return $current;
}
+ // Returns a key, ignoring the |$this->throw_undefined_errors| setting and
+ // returning NULL if not found.
+ public function getSilent($key)
+ {
+ $value = null;
+ try
+ {
+ $value = $this->get($key);
+ }
+ catch (UndefinedKeyException $e)
+ {}
+ return $value;
+ }
+
// Sets a value for a given key.
public function set($key, $value)
{
diff --git a/input/form_key.php b/input/form_key.php
index a7d4695..2327d8a 100644
--- a/input/form_key.php
+++ b/input/form_key.php
@@ -137,22 +137,8 @@ class ValidateFormKeyEvent extends \phalanx\events\Event
public function handle()
{
- $valid = true;
- $key = '';
-
- try
- {
- $key = $this->context()->gpc()->get($this->post_variable);
- }
- catch (\phalanx\base\UndefinedKeyException $e)
- {
- $valid = false;
- }
-
- if (!$this->manager->validate($key))
- $valid = false;
-
- if (!$valid)
+ $key = $this->context()->gpc()->getSilent('p.' . $this->post_variable);
+ if (!$key || !$this->manager->validate($key))
throw new FormKeyException('Form key "' . $key . '" did not validate.');
}
}
diff --git a/testing/tests/base/key_descender_test.php b/testing/tests/base/key_descender_test.php
index b71815d..fe0cd08 100644
--- a/testing/tests/base/key_descender_test.php
+++ b/testing/tests/base/key_descender_test.php
@@ -99,6 +99,21 @@ class KeyDescenderTest extends \PHPUnit_Framework_TestCase
$this->assertNull($desc->get('undefined.key'));
}
+ public function testGetSilent()
+ {
+ $array = array();
+ $desc = new KeyDescender($array);
+ $desc->set_throw_undefined_errors(true);
+ try
+ {
+ $this->assertNull($desc->getSilent('undefined_key'));
+ }
+ catch (\phalanx\base\UndefinedKeyException $e)
+ {
+ $this->fail('unexpected \phalanx\base\UndefinedKeyException');
+ }
+ }
+
public function testSetSingleLevelArray()
{
$array = array('foo' => 'bar');