Php – Zend Framework 1.10.7 telephone validator

PHPzend-framework

Please See Correct Answer for solution to the requested question.

Hi,
Recently I have been searching for telephone validation in zend framework which I think is a missing component of their Validator framework. Therefore I created custom telephone validator which I would like to share with you.


Put code below in a file accessible by require_once php statement. Here we suppose that this code is pasted in file telephoneValidator.php.

class Custom_Validator_Telephone extends Zend_Validate_Abstract
{
 const INVALID = 'This field is required';
 protected $_messageTemplates = array(
        self::INVALID => "Incorrect telephone number"
 );
 public function __construct()
 {
 }
 public function isValid($value) 
 {
  if(preg_match("/^(\+)?(\([0-9]+\)\-?\s?)*([0-9]+\-[0-9]+)*([0-9]+)*$/", trim($value))) 
  {
   return true;
  }  
  else
  {
   $this->_error(self::INVALID);
   return false;
  }
 }
}

How to Use it: Put $tel Zend_Element below in your Zend_Form object with addElement method

require_once("telephoneValidator.php")
$tel = new Zend_Form_Element_Text($fieldName);
$telValidator = new Custom_Validator_Telephone();

$tel->addValidator($telValidator, true)
    ->setAllowEmpty(false)
    ->addValidator('NotEmpty', true, array('messages' => array(
                    'isEmpty' => $label.' is required')))   
 ->setLabel("Telephone Number");

$form->addElement($tel);

Error message from this validator can be modified using setMessage method of Zend_Validate_Abstract class

$telValidator->setMessage("%value% is not correct telephone number");
$tel->addValidator($telValidator, true)

This validator is working fine with phone numbers in following format

   +(92) 345-5141637
   +(92)-345-5141637 
   (92) 345-5141637
   (92)-345-5141637
   +(92)-345-5141637 
   92-345-5141637
   +92-345-5141637
   +923455141637 
   923455141637 
   (92)-(345)-5141637

I have'nt put length check yet on phone number but it will require to create a filter for filtering digits from the input telephone phone number then using StringLength
validator.
Although I am new in Zend framework, I would like to know that how can I automatically include my classes in custom folders inside application folder using autoloader of Zend framework. For example I have my custom classes in MajorClasses folder inside application folder, please tell me the way to automatically include all the classes inside my MajorClasses folder just be specifying its name because there can be many files inside that folder but I want them to be included automatically. Is this possible in Zend framework?

Best Answer

Why did you post your full telphone stuff? Your question is just how do you enable autoload of custom files in Zend? Right?

In Zend 1.10.7 you can add the following to public/index.php ABOVE your bootstrap->run command

require_once "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Custom');

You can register as many custom namespaces as you like. In this case Custom is a new namespace thus your classes should be named as follows.

class Custom_Validator_Telephone extends Zend_Validate_Abstract

Now about your directory structure, first question your MajorClasses folder is inside application/??? if so ok, in the same file, as above, there should be a set_include_path() function being run. Within it your setting your library path, now we can add the path to your new directory.

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
    APPLICATION_PATH.'/MajorClasses'.PATH_SEPARATOR,
)));

WITHIN MajorClasses folder you will NOW have to create a directory FOR EACH namespace. So if you have the namespace Custom, you create the directory, also you have to create the Validator directory since you're naming it like that, so your path would be.

application/MajorClasses/Custom/Validator/Telephone.php

Telephone.php should be the name of your class file, the class filename is always the last namespace in the classname.

Did I miss anything?

Related Topic