currentlyOffline

Square brackets in HTML element name – Zend Framework

November 22nd, 2008 currentlyOffline

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.

Posted in Zend Framework | 1 Comment »

Custom URLs in Zend Framework (v1.6.1)

November 11th, 2008 currentlyOffline

If you are having problems with custom URLs in Zend Framework – you’re not alone.

Recently I was rewriting one of my websites using ZF. The site is old, well ranked in search engines. In a situation like this you don’t really want to change URL structure. So i started to play with Apache rewrite rules and got stuck.

The problem is, that by default HTTP request object is using $_SERVER['REQUEST_URI'] which means that even after setting rule like

RewriteRule ^articles/(.*)$ articles/view/url/$1 [L]

won’t work, because $_SERVER['REQUEST_URI'] remains unchanged.

To fix this, I’ve changed Http.php in library/Zend/Controller/Request/ and on line 391(should be the same if you are using same version as me, or look for public function setRequestUri($requestUri = null) ) and replaced

389:            if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
390:                $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
391:            }elseif (isset($_SERVER['REQUEST_URI'])) {
394:                $requestUri = $_SERVER['REQUEST_URI'];
395:            }elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI

with:

389:            if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
390:                $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
391:            }elseif(isset($_SERVER['PATH_INFO'])){
392:                     $requestUri = $_SERVER['PATH_INFO'];
393:            }elseif (isset($_SERVER['REQUEST_URI'])) {
394:                $requestUri = $_SERVER['REQUEST_URI'];
395:            } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI

It’s a quite dirty hack because you have to change core files and need to remember that before updating ZF to a new version, but it does the job.

If you know a better solution – please let me know!

Posted in Zend Framework | 1 Comment »

Zend Framework – A table must have a primary key, but none was found

November 9th, 2008 currentlyOffline

If you’re beginner using Zend_Db_Table with VIEWs and starting to hate Zend framework because of this message:

Fatal error: Uncaught exception ‘Zend_Db_Table_Exception’ with message ‘A table must have a primary key, but none was found’ in…..

Remember, that you can always specify primary key:

<?php
class Articles extends Zend_Db_Table{
protected $_name = ‘v_articles’;
protected $_primary = ‘id’;

Posted in Zend Framework | 3 Comments »

  • You are currently browsing the currentlyOffline weblog archives for November, 2008.

  • 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