About Chris Morrell

I am a Philadelphia web designer and developer who focuses on PHP development and usable design. I am also the Director of IT for the International Association of Certified Home Inspectors.

Please Note: My site fell victim to a Wordpress security flaw a few weeks ago, and I'm just getting everything back to normal. Please bear with me.

I am currently not accepting any new clients.

Other Sites/Clients

Contact Me

If you need to get in touch with me, my name is Chris and my domain name is cmorrell.com. Think about it.

Zend Framework URI validator & filter

Posted by Chris Morrell on March 12th, 2010 in Zend Framework

For the last couple of months I’ve been incorporating portions of applications I’m working on into my Galahad Framework Extension project.  Right now it’s not at a point where I’d feel comfortable promoting it (you can check out the project on GitHub if you want), but there are portions that are pretty solid that might be useful to others right now.  Two such portions are Galahad_Validate_Uri and Galahad_Filter_PrependHttp which are both very useful for processing forms with URL fields.

Recently I was building a form that contained a URL field, and after browsing the Zend_Validate docs, I was surprised not to find a Zend_Validate_Uri component.  Luckily, Zend_Uri already validates URIs, so it was just a matter of writing a wrapper that followed the Zend_Validate APIs.  Basic usage:

$validator = new Galahad_Validate_Uri();
$validator->isValid('http://www.google.com/');

Obviously this would be much more useful when combined with Zend_Form or some more extensive validation chains, but you get the point.

Grab Galahad_Validate_Uri from GitHub.

The second issue I often deal with is people forgetting (or not knowing) to include http:// in the URLs that they submit.  Rather than solve this at the controller-level, I decided this was a common enough problem to build a Zend_Filter component for.  Basic usage:

$filter = new Galahad_Filter_PrependHttp(array(
    'allowedSchemes' => array('http://', 'https://', 'mailto:'),
    'checkUri' => true,
));
echo $filter->filter('google.com'); // Prints 'http://google.com'

This filter takes any string and prepends “http://” to it if it doesn’t already contain an allowed scheme (more on that below).  By default it allows “http://”, “https://” and “mailto:” schemes, but you could set this to anything (say you want to allow “itms://” [iTunes] links) by setting the ‘allowedSchemes’ option.  It also (optionally) checks if the resulting URI is valid, and only applies the filter if it is (effectively only prepending “http://” to otherwise valid URIs).  Remember, though, that “http://something” is technically a valid URI, so that will be accepted.  In the future I hope to add additional options to limit to URLs that follow certain standards.

Grab Galahad_Filter_PrependHttp from GitHub.

Update: I’ve submitted Zend_Filter_PrependHttp to the Zend Framework wiki for comments.  Check it out and let me know if you think there’s anything I should change (I’ve posted a few ideas already).

The validator and filter work well together with Zend_Form to create URL form elements:

$this->addElement('text', 'my_url', array(
    'label' => 'URL:',
    'filters' => array('StringTrim', 'StringToLower', new Galahad_Filter_PrependHttp()),
    'validators' => array(new Galahad_Validate_Uri()),
));

The form element generated by that code will produce fairly normalized, valid URIs.

2 Comments »

2 Tweets

2 Responses to “Zend Framework URI validator & filter”

  1. newmediahub

    Chris Morrell posted: Zend Framework URI validator & filter http://cmorrell.com/web-development/zf/validate-filter-url-728

    This comment was originally posted on Twitter

  2. papayasoft

    Nice! RT @newmediahub: Chris Morrell posted: Zend Framework URI validator & filter http://bit.ly/9h82dG

    This comment was originally posted on Twitter

Leave a Reply

Additional comments powered by BackType

Comments & Trackbacks

You can leave a response, or trackback from your own site. Follow any responses to this entry through the RSS 2.0 feed.

More...

@inxilpro

  • Wet Hot American Summer at World Cafe Live. It's free and dead. 2 days ago
  • Would really like something that combines Twitter starred + Google Reader starred/liked + Delicious.com. Does such a thing exist? 6 days ago
  • Also, how can OS X be so rock solid and other Apple-developed software be so buggy? 1 week ago
  • More updates...
Copyright © Chris Morrell, Powered by WordPress, Entry RSS Feed / Comment RSS Feed