-- Steve Clay <[EMAIL PROTECTED]> wrote
(on Wednesday, 26 March 2008, 04:53 PM -0400):
> I'm trying the addElement shorthand straight out of the documentation [1]
> and getting a fatal error.
>
> require_once 'Zend/Form.php';
>
> $form = new Zend_Form;
> $form->addElement('text', 'username', array(
> 'validators' => array(
> 'alnum',
> array('regex', false, '/^[a-z]/i')
> ),
> 'required' => true,
> 'filters' => array('StringToLower'),
> ));
>
> result: Fatal error: Cannot unset string offsets in
> /var/www/html/_includes/Zend/Form/Element.php on line 991
>
> PHP 5.1.6, Element.php is version 8983 2008-03-21 (a snapshot), but 1.5.0
> had the same error.
>
> The error is due to this code (line 989):
>
> if (isset($options['messages'])) {
> $messages = $options['messages'];
> unset($options['messages']);
> }
>
> The problem is isset($options['messages']) returns true even though
> $options is the string '/^[a-z]/i'. Inside isset(), PHP casts 'messages' to
> an int, so it's the same as isset($options[0]), which is the character '/'!
> Since the isset passes, unset fails because you can't unset a character
> from a string.
>
> My feeling is that isset should be fixed, but for the time being, a
> Zend_Form_Element fix is simple:
>
> if (is_array($options) && isset($options['messages'])) {
> $messages = $options['messages'];
> unset($options['messages']);
> }
There are two things going on here. First, the supported way of passing
constructor options in Zend_Form_Element::addValidator() is to pass an
array of constructor values. So, you're getting this error because
you're passing things incorrectly. Try this instead:
array('regex', false, array('/^[a-z]/i'))
However, obviously the Quick Start is incorrect, so please file an
Issue in the tracker noting this -- and I'll fix the docs, as well as
add an explicit cast of the constructor options to an array.
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/