Zend_Validate_StringEquals
Jan0
If you ever wonder where that 'StringEquals' validator rule taken as example from the Zend_Filter_Input documentation page results in an error like below, well read again. It was clearly stated as 'hypothetical'.
Plugin by name 'StringEquals' was not found in the registry; used paths: Zend_Validate_: Zend/Validate/
Given such validator would be useful on a number of situations i.e. confirming passwords, emails, etc. I present to you my own version of the class.
<?php class Zend_Validate_StringEquals extends Zend_Validate_Abstract { const NOT_EQUAL = 'stringNotEqual'; const MISSING = 'stringMissing'; /** * @var array */ self::NOT_EQUAL => "%field1% and %field2% are not equal.", self::MISSING => "One or both strings are missing." ); /** * @var array */ 'field1' => '_field1', 'field2' => '_field2' ); protected $_case = false; protected $_field1 = null; protected $_field2 = null; /** * Sets validator options * * @param boolean $case * @return void */ public function __construct($case = false) { $this->_case = $case; } /** * Defined by Zend_Validate_Interface * * Returns true if and only if the the 2 strings are equal * * @param array $value * @return boolean */ public function isValid($value) { $this->_error(self::MISSING); } if($this->_case === true) $function = 'strcmp'; else $function = 'strcasecmp'; if(0 !== $function($this->_field1,$this->_field2)) $this->_error(self::NOT_EQUAL); return false; } else { return true; } } }
Here is a sample test case. Validate password and confirm password elements represented by 'password' and 'cpassword' element names respectively.
'presence' => 'required', 'fields' => 'password', 'messages' => "Passwords must be between 5 and 15 characters in length."), Zend_Validate_StringEquals::NOT_EQUAL => "Passwords does not match.", Zend_Validate_StringEquals::MISSING => "Both password fields must be filled.")))); $inputdata = new Zend_Filter_Input($filter,$validators,$_POST,$options);
FFMpeg-PHP: undefined symbol: php_gd_gdImageSetPixel
Oct3
I was recently updating ffmpeg-php on one of our servers to the latest SVN release of the 0.6.3 branch. On a 64bit CentOS 5.3 with PHP 5.2.11, the extension compiled and installed fine however Apache will not load it and spit out the error below:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/ffmpeg.so' - /usr/lib64/php/modules/ffmpeg.so: undefined symbol: php_gd_gdImageSetPixel in Unknown on line 0
Surely enough, the GD extension was there, but why is ffmpeg complaining about not finding that shared symbol? Because, ffmpeg is loading first than GD (alphabetically) and such symbol has not been loaded. After adding the GD extension line on top of ffmpeg making sure it loads first the error went away and all is well again.
CodeIgniter and Smarty
Jul0
I've been used to working on projects using Smarty, its flexibility and most of the HTML designers and graphics artists I have worked with are only familiar with it. Until recently I was compelled by my co-worker to use a framework and he started using CodeIgniter without our prior approval and knowledge. To save time, I have to deal with it and continue with the CI path. Fortunately, CI can be simply extended from its Models, Controllers, libraries even its core files so it was not a big deal tying Smarty and CI together. This is how we do it.
According to the CI User Guide -> Creating Libraries, you can create your own library or extend/replace an existing native CI library. So far we need to create our own and extend from Smarty. SO in our application folder we add our Smarty library like shown in the folder structure below:
+/system
|
+----/application
|
+----/libraries
|
+----/smarty
| |
| +----/internals
| +----/plugins
| +----/Config_File.class.php
| +----/debug.tpl
| +----/Smarty.class.php
| +----/Smarty_Compile.class.php
+----/JHSmarty.php
+----/JHController.php
The above tree is only the part we are concerned, tying up Smarty with CI using libraries. You will notice two additional files, JHSmarty.php and JHController.php. Let us deal with JHSmarty.php first.
JHSmarty.php will serve as you CI custom library, it will extend the original Smarty class and we will add some additional functions for more flexibility. Consider the following example JHSmarty.php file.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
require_once "smarty/Smarty.class.php";
class JHSmarty extends Smarty
{
public $CI = null;
public function __construct()
{
parent::Smarty();
}
public function view($resource_name, $cache_id = null) {
if (strpos($resource_name, '.') === false) {
$resource_name .= '.tpl';
}
return parent::display($resource_name, $cache_id);
}
public public function register_ci()
{
$this->CI =& get_instance();
}
}
?>
Now to breakdown each component:
__construct : As any PHP class we need a constructor, in this case we as well initialize our parent class by calling parent::Smarty();.
view : This is a simple implementation of the Smarty::display() function wherein we can optinally pass only the name part of a template file without the ".tpl" extension. This can be useful if you are presenting static pages self contained on a template.
register_ci : This function registers an instance of the CI base object into Smarty, meaning including all methods, variables and objects that you will normally find in your controller. Except in this case when you call the function the CI instance will be available as i.e. $this->CI->load->model('Somemodel','somemodel');. This is very useful if you are adding custom modifiers, plugins, etc to Smarty and you want to tap into existing resources in the CI object. Since smarty will pass itself to any plugin, modifier or outputfilter you can access it like this from any of them:
$smarty->CI->load->model('Somemodel','somemodel');
Also, you might want to autoload you JHSmarty library as you will be using your templating engine all through out your project. To automatically load your library just add the class name in all lowercase to your /system/application/config/autoload.php
$autoload['libraries'] = array('jhsmarty');
So in your controller you can always reference your custom library as:
$this->jhsmarty
Now, let's go to the next file, JHController.php. The file is an extension of the native Controller class that comes with CI. Why would I need one? Simply because I need to control some aspects of my site on the Controller level i.e. Auth checking, loading global variables, etc.
One important note is, the JH prefix on the controller name (JH on the smarty library does not count I just used it for consistency). You must define your subclass prefix for CI to use them, /system/application/config/config.php contains the relative configuration.
$config['subclass_prefix'] = 'JH';
So what do I have on my extended Controller:
The defaultview variable : Since my extended native controller will be used on all further controllers within my project, I can use this variable to assign the template I will be using to display the current request.
The finalize method : This method will serve as my Smarty::display() version except that I can assign as much variables and control the further display of the defaultview variable. I can also trap errors and display an error page if needed.
I hope you find this article useful, mainly I wrote this for a new programmer I am working with. If you have any questions/comments feel free to drop a comment below.












