Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:51:52+00:00 2026-05-16T05:51:52+00:00

Please See Correct Answer for solution to the requested question. Hi, Recently I have

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:51:53+00:00Added an answer on May 16, 2026 at 5:51 am

    This question comes under Zend Resource auotloading http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html

    In short, in order to include all files under particular folder we need to follow following rules.

    1) Suppose all files under MajorClasses folder are started by Custom i.e. class Custom_validator_Telephone, so our namespace for this folder is Custom. In order to include files under this folder we need to create an instance of zend resource autoloader

    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
              'basePath'  => "/path/to/MajorClasses",
              'namespace' => 'Custom'
          ));
    

    2) Now we have our resource autoloader ready, we need to add resources to this object for example if I have folder with name validators inside MajorClasses folder and all files inside of this folder are prefixed by Custom_Validator then namespace of this folder is Validator because we have already defined Custom as prefix of the parent resource object.

    $resourceLoader->addResourceType('validator', 'validators/', 'Validator');
    

    Here

    • 1st parameter says about name of resource we are adding and is used for internal recognition.
    • 2nd parameter defines path of the folder relative to the base Path we declared when instantiating resource autoloader object, so path of this resource is /path/to/MajorClasses/validators/.
    • 3rd parameter specifies namespace of the class i.e. it will be concatenated by the resource object’s namespace(in our case it is Custom) so prefix of complete class upto this point is Custom_Validator and php file inside this folder will be postfixed with this class name after stripping .php file extension

    3) Now we can put Telephone.php inside validators folder and if we place above code in bootstrap’s any function for e.g. _initPlaceHolders then we can create instance of Custom_Validator_Telephone anywhere in our application without need of using require_once statement.

    $telValidator = new Custom_Validator_Telephone();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.