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.
Additional comments powered by BackType
You can leave a response, or trackback from your own site. Follow any responses to this entry through the RSS 2.0 feed.
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
Nice! RT @newmediahub: Chris Morrell posted: Zend Framework URI validator & filter http://bit.ly/9h82dG
This comment was originally posted on Twitter