Sfoglia il codice sorgente

[ZF-8489] Zend_Validate_CallBack:

- fixed a "bug" which did not allow to use self-defined additional parameters by isValid()
- note that this change breaks BC and is therefor not backported to 1.9

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19529 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 anni fa
parent
commit
a4303f43a5

+ 18 - 2
documentation/manual/en/module_specs/Zend_Validate-Callback.xml

@@ -1,7 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- Reviewed: no -->
 <sect2 id="zend.validate.set.callback">
-
     <title>Callback</title>
 
     <para>
@@ -209,8 +208,25 @@ if ($valid->isValid($input)) {
 ]]></programlisting>
 
         <para>
+            When there are additional values given to <methodname>isValid()</methodname> then these
+            values will be added immediately after <varname>$value</varname>.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$valid = new Zend_Validate_Callback(array('MyClass', 'myMethod'));
+$valid->setOptions($option);
+
+if ($valid->isValid($input, $additional)) {
+    // input appears to be valid
+} else {
+    // input is invalid
+}
+]]></programlisting>
+
+        <para>
             When making the call to the callback, the value to be validated will always be passed as
-            the first argument to the callback; all other options will follow it. The amount and
+            the first argument to the callback followed by all other values given to
+            <methodname>isValid()</methodname>; all other options will follow it. The amount and
             type of options which can be used is not limited.
         </para>
     </sect3>

+ 3 - 3
library/Zend/Validate/Callback.php

@@ -154,10 +154,10 @@ class Zend_Validate_Callback extends Zend_Validate_Abstract
     {
         $this->_setValue($value);
 
-        $options = $this->getOptions();
-        array_unshift($options, $value);
-
+        $options  = $this->getOptions();
         $callback = $this->getCallback();
+        $args     = func_get_args();
+        $options  = array_merge($args, $options);
 
         try {
             if (!call_user_func_array($callback, $options)) {

+ 14 - 0
tests/Zend/Validate/CallbackTest.php

@@ -106,6 +106,13 @@ class Zend_Validate_CallbackTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    public function testAddingValueOptions()
+    {
+        $valid = new Zend_Validate_Callback(array('callback' => array($this, 'optionsCallback'), 'options' => 'options'));
+        $this->assertEquals(array('options'), $valid->getOptions());
+        $this->assertTrue($valid->isValid('test', 'something'));
+    }
+
     public function objectCallback($value)
     {
         return true;
@@ -115,6 +122,13 @@ class Zend_Validate_CallbackTest extends PHPUnit_Framework_TestCase
     {
         return true;
     }
+
+    public function optionsCallback($value)
+    {
+        $args = func_get_args();
+        $this->assertContains('something', $args);
+        return $args;
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Validate_CallbackTest::main') {