ZF-6822: In example Form create element - param match in validator Regex must be an Array not String

Description

Little fix problem in documentation.

http://framework.zend.com/manual/en/…

Passage - The final element setup might look like this: ... // or, more compactly: $username->addValidators(array('alnum', array('regex', false, '/^[a-z]/i') )) ->setRequired(true) ->addFilters(array('StringToLower'));

Fixed: ... array('regex', false, array('/^[a-z]/i')) ...

Comments

Confirmed in current trunk


Index: documentation/manual/en/module_specs/Zend_Form-QuickStart.xml
===================================================================
--- documentation/manual/en/module_specs/Zend_Form-QuickStart.xml       (Revision 21764)
+++ documentation/manual/en/module_specs/Zend_Form-QuickStart.xml       (Arbeitskopie)
@@ -183,7 +183,7 @@
 
 // or, more compactly:
 $username->addValidators(array('alnum',
-        array('regex', false, '/^[a-z]/i')
+        array('regex', false, array('/^[a-z]/i'))
     ))
     ->setRequired(true)
     ->addFilters(array('StringToLower'));
@@ -202,7 +202,7 @@
 $form->addElement('text', 'username', array(
     'validators' => array(
         'alnum',
-        array('regex', false, '/^[a-z]/i')
+        array('regex', false, array('/^[a-z]/i'))
     ),
     'required' => true,
     'filters'  => array('StringToLower'),

After talking with Thomas, there is an error within Zend_Form_Element, namely in method _loadValidators()


isset($validator['options']['messages']) === true
// even when 'messages' is not set within $validator['options']

Weird thing, isset only checks the first key in an associative array??

Fix Patch, including Unit Test:


Index: tests/Zend/Form/ElementTest.php
===================================================================
--- tests/Zend/Form/ElementTest.php     (Revision 21764)
+++ tests/Zend/Form/ElementTest.php     (Arbeitskopie)
@@ -746,6 +746,11 @@
         $this->assertEquals(1, $order['Zend_Validate_Alnum'], var_export($order, 1));
     }
 
+    public function testValidatorOptionsNonexistantMessages()
+    {
+        $this->element->addValidator('regex','false','/^.$/');
+        $this->assertTrue($this->element->isValid('a'));
+    }
 
     public function testCanAddMultipleValidators()
     {
Index: library/Zend/Form/Element.php
===================================================================
--- library/Zend/Form/Element.php       (Revision 21764)
+++ library/Zend/Form/Element.php       (Arbeitskopie)
@@ -2099,7 +2099,7 @@
         }
 
         $messages = false;
-        if (isset($validator['options']['messages'])) {
+        if (isset($validator['options']) && array_key_exists('messages', (array)$validator['options'])) {
             $messages = $validator['options']['messages'];
             unset($validator['options']['messages']);
         }

Fixed with r21873 Note that the string-notation works. There was a failure within the class.