currentlyOffline

Square brackets in HTML element name – Zend Framework

I was having problems with square brackets in Zend_Form_Element_File – doesn’t matter how hard I’ve tried, any non alphanumeric characters where stripped out.
After some investigation, i’ve found, that the method setName in Zend_Form_Element class is not using second parameter of filterName – member of the same class

408:    /**
409:     * Filter a name to only allow valid variable characters
410:     * 
411:     * @param  string $value 
412:     * @param  bool $allowBrackets
413:     * @return string
414:     */
415:    public function filterName($value, $allowBrackets = false)
416:    {
417:        $charset = '^a-zA-Z0-9_\x7f-\xff';
418:        if ($allowBrackets) {
419:            $charset .= '\[\]';
420:        }
421:        return preg_replace('/[' . $charset . ']/', '', (string) $value);
422:    }
423:
424:    /**
425:     * Set element name
426:     * 
427:     * @param  string $name 
428:     * @return Zend_Form_Element
429:     */
430:    public function setName($name)
431:    {
432:        $name = $this->filterName($name);
433:        if ('' === $name) {
434:            require_once 'Zend/Form/Exception.php';
435:            throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
436:        }
437:
438:        $this->_name = $name;
439:        return $this;
440:    }

Line 432 is the one to look at :)

To solve this problem, i created another class file element extending original class:
class My_Form_Element_File extends Zend_Form_Element_File{
and just replaced setName($name) method with the same code, except one line where it’s calling filterName, where i’ve added second parameter, telling to leave square brackets alone :)

with something like this:

430: public function setName($name)
431: {
432: $name = $this->filterName($name, true);

It was enough for me because i’m only using brackets for file element.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

This entry was posted on Saturday, November 22nd, 2008 at 10:27 pm and is filed under Zend Framework. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

« Custom URLs in Zend Framework (v1.6.1)
A free Zend Framework Book »

One Response to “Square brackets in HTML element name – Zend Framework”

  1. Bart Says:
    January 21st, 2010 at 7:23 pm

    Hi!
    Correct solution (see comments):
    http://zendframework.com/issues/browse/ZF-5556?focusedCommentId=28237&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_28237

Leave a Reply

  • Pages

    • About
  • Archives

    • December 2009
    • November 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • February 2009
    • January 2009
    • November 2008
  • Categories

    • books (3)
    • Cristina Scabbia (1)
    • I hate computers… and Internet! (6)
    • Jokes (1)
    • jQuery (1)
    • Music (1)
      • italian metal (1)
      • Lacuna Coil (1)
    • Question – Answer (1)
    • randomness (3)
    • Uncategorized (2)
    • web hosting jokes (3)
    • Zend Framework (6)

currentlyOffline is proudly powered by WordPress | Bob