So recently at work I have been doing a lot of work with the Zend Framework, but I was having some issues with magic_quotes_gpc
being turned on in the PHP configuration of the server. The easy thing to do would be to disable magic_quotes_gpc in the php.ini and that solves the issue. However this was not an option in my case, as I didn’t want to fiddle with the configuration in case it broke anything else.
I had a look around for a solution and found one on Phil Brown’s Web Development Blog his post
Zend Framework Forms and magic_quotes_gpc which explains how to use Zend Filters to combat this issue. His solution works perfectly but you have to set the filter for each form element individually and I found this a bit of a chaw.
So I looked around to find a way of applying it to all elements within the form.
Here is the solution I came up with this is all the code you need to get it working. I am doing this for the Strip Slashes filter but will work with any standard or non standard filters.
First you want to add the filter to your library. So create in your library directory the folder `MoreCowbell` then inside that the folder `Filter` and finally create the file `StripSlashes.php` which contains the following.
<?php //FILE: library/MoreCowbell/Filter/StripSlashes.php class MoreCowbell_Filter_StripSlashes implements Zend_Filter_Interface { public function filter($value) { return get_magic_quotes_gpc() ? $this->_clean($value) : $value; } protected function _clean($value) { return is_array($value) ? array_map(array($this, '_clean'), $value) : stripslashes($value); } } ?>
Next you need to create a class that extends Zend_Form. In my case I have created it and added it as part of my Pablumfication library.
<?php //FILE: library/Pablumfication/Zend/Form.php class Pablumfication_Zend_Form extends Zend_Form { // Configure path to custom plugins public $elementPrefixPaths = array('filter' => array( 'prefix' => 'MoreCowbell_Filter', 'path' => 'MoreCowbell/Filter' )); // Shortcut to default element filters public $elementFilters = array( 'StripSlashes' ); public function __construct($options = null) { parent::__construct($options); $this->addElementPrefixPaths($this->elementPrefixPaths); $this->setElementFilters($this->elementFilters); } } ?>
Once that’s done its simply a case of a ensuring that any forms you create extend this one. Then when the form is constructed it will automatically apply the correct element filters and element prefixs to each element within the form.
<?php class Application_Form_Article extends Pablumfication_Zend_Form { public function init() { $this->setName('ArticleForm'); $this->setMethod('post'); $this->addElement('text', 'heading', array( 'required' => true, 'label' => 'Heading:' )); $this->addElement('textarea', 'content', array( 'required' => true, 'label' => 'Content:' 'cols' => 55, 'rows' => 16 )); $this->addElement('text', 'photoS', array( 'required' => true, 'label' => 'Photo Small:' )); $this->addElement('text', 'photoL', array( 'required' => true, 'label' => 'Photo Large:' )); $this->addElement('submit', 'Submit'); } } ?>
At the moment its only the strip slashes filter that is automatically applied but if for instance you wanted StringTrim to be applied to every form element you would just modify. The declaration of `elementFilters`.
public $elementFilters = array( 'StripSlashes' );
to
public $elementFilters = array( 'StripSlashes', 'StringTrim' );
Recent Comments