Zend_Validate_StringEquals

23
Jan
0

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.

  1.  
  2. <?php
  3. class Zend_Validate_StringEquals extends Zend_Validate_Abstract
  4. {
  5. const NOT_EQUAL = 'stringNotEqual';
  6. const MISSING = 'stringMissing';
  7.  
  8. /**
  9.   * @var array
  10.   */
  11. protected $_messageTemplates = array(
  12. self::NOT_EQUAL => "%field1% and %field2% are not equal.",
  13. self::MISSING => "One or both strings are missing."
  14. );
  15.  
  16. /**
  17.   * @var array
  18.   */
  19. protected $_messageVariables = array(
  20. 'field1' => '_field1',
  21. 'field2' => '_field2'
  22. );
  23.  
  24. protected $_case = false;
  25. protected $_field1 = null;
  26. protected $_field2 = null;
  27.  
  28. /**
  29.   * Sets validator options
  30.   *
  31.   * @param boolean $case
  32.   * @return void
  33.   */
  34. public function __construct($case = false)
  35. {
  36. $this->_case = $case;
  37. }
  38.  
  39. /**
  40.   * Defined by Zend_Validate_Interface
  41.   *
  42.   * Returns true if and only if the the 2 strings are equal
  43.   *
  44.   * @param array $value
  45.   * @return boolean
  46.   */
  47. public function isValid($value)
  48. {
  49. if(!is_array($value) OR sizeof($value) < 2) {
  50. $this->_error(self::MISSING);
  51. }
  52.  
  53. $this->_field1 = array_shift($value);
  54. $this->_field2 = array_shift($value);
  55.  
  56. if($this->_case === true) $function = 'strcmp';
  57. else $function = 'strcasecmp';
  58.  
  59. if(0 !== $function($this->_field1,$this->_field2)) $this->_error(self::NOT_EQUAL);
  60.  
  61. if (count($this->_messages)) {
  62. return false;
  63. } else {
  64. return true;
  65. }
  66. }
  67. }
  68.  

Here is a sample test case. Validate password and confirm password elements represented by 'password' and 'cpassword' element names respectively.

  1.  
  2. $filters = array('password' => 'StringTrim', 'cpassword' => 'StringTrim');
  3. $validators = array(
  4. 'Password' => array(
  5. 'presence' => 'required',
  6. array('StringLength',5,15),
  7. 'fields' => 'password',
  8. 'messages' => "Passwords must be between 5 and 15 characters in length."),
  9. 'Confirm password' => array(
  10. array('StringEquals'),
  11. 'fields' => array('password','cpassword'),
  12. 'messages' => array(
  13. 0 => array(
  14. Zend_Validate_StringEquals::NOT_EQUAL => "Passwords does not match.",
  15. Zend_Validate_StringEquals::MISSING => "Both password fields must be filled."))));
  16.  
  17. $inputdata = new Zend_Filter_Input($filter,$validators,$_POST,$options);
  18.  

Fetch Smarty Templates From MySQL Database

22
Mar
0

I was recently working on a mailing application. With the nature of the bulk mailing routine, it was designed to store templates made by its users and email designers. Since most of them have been exposed to Smarty templating before we decided to base our email template parsing to Smarty.

Smarty does not come with the ability to parse templates from database natively, but it is easy to extend it to do just that.

DISCLAIMER: This is a quick and dirty hack I provided with my colleagues. Improvement required!

This approach should work on both PHP 4 and PHP 5, since Smarty.class.php is written on PHP 4.

What we will need to do is extend Smarty.class.php on a new class and override function _fetch_resource_info and replace the if clause demonstrated below to use our code that actually fetches database stored templates.

class Smarty
{
    function _fetch_resource_info(&$params) {
        ....
        if ($this->_parse_resource_name($_params)) {
            .....
        }
        ....
    }
}

Our resulting class would look something like:

require_once 'Smarty.class.php';

class Smarty_Db extends Smarty
{
    function _fetch_resource_info(&$params) {
        ....
        $_resource_type = $_params['resource_type'];
        $_resource_name = $_params['resource_name'];

	$db = new Template_Db;
	if ($params['get_source']) {
            $params['source_content'] = $db->fetchTemplate($myTemplateId);
        }
	$params['resource_timestamp'] = now();
        $_return = true;
        ....
    }
}

So you would then use your Smarty extended class like:

$smarty = new Smarty_Db;
$smarty->compile_dir = '/path/to/my/compile/dir';

$compiledTemplateFromDb = $smarty->fetch($myTemplateId);

That is all there is to it actually. I haven't went through all the Smarty pre and post processing, but this should suffice the requirements for now.

Hope this helps.

CSS Gradient Text Effect

4
Jan
0

This is a very useful and cool CSS design approach, saves time and effort and still SEO friendly. Hit the link for more.

Do you want to create fancy headings without rendering each heading with Photoshop? Here is a simple CSS trick to show you how to create gradient text effect with a PNG image (pure CSS, no Javascript or Flash). All you need is an empty tag in the heading and apply the background image overlay using the CSS position:absolute property. This trick has been tested on most browsers: Firefox, Safari, Opera, and even Internet Explorer 6. Continue to read this article to find out how.

http://www.webdesignerwall.com/tutorials/css-gradient-text-effect/