简体   繁体   English

Zend Framework 1.10.7电话验证器

[英]Zend Framework 1.10.7 telephone validator

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. 嗨,最近,我一直在zend框架中搜索电话验证,我认为这是其Validator框架中缺少的组件。 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. 将下面的代码放在一个require_once php语句可访问的文件中。 Here we suppose that this code is pasted in file telephoneValidator.php. 在这里,我们假设此代码粘贴在文件phoneValidator.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 如何使用:使用addElement方法将$ tel Zend_Element放在您的Zend_Form对象中

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 可以使用Zend_Validate_Abstract类的setMessage方法修改来自此验证器的错误消息

$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. 我还没有对电话号码进行长度检查,但是它将需要创建一个过滤器来过滤输入电话号码中的数字,然后使用StringLength验证程序。 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. 尽管我是Zend框架的新手,但我想知道如何使用Zend框架的自动加载器自动将类包含在应用程序文件夹中的自定义文件 夹中 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. 例如,我在应用程序文件夹的MajorClasses文件夹中有我的自定义类,请告诉我自动指定我的MajorClasses文件夹中的所有类的方法,只需指定其名称,因为该文件夹中可能有很多文件,但我希望包含它们自动。 Is this possible in Zend framework? 在Zend框架中这可能吗?

Why did you post your full telphone stuff? 您为什么张贴完整的电话资料? Your question is just how do you enable autoload of custom files in Zend? 您的问题就是如何在Zend中启用自定义文件的自动加载? Right? 对?

In Zend 1.10.7 you can add the following to public/index.php ABOVE your bootstrap->run command 在Zend 1.10.7中,您可以在bootstrap-> run命令上方将以下内容添加到public / index.php中

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. 在这种情况下,Custom是一个新的命名空间,因此您的类应按以下方式命名。

class Custom_Validator_Telephone extends Zend_Validate_Abstract

Now about your directory structure, first question your MajorClasses folder is inside application/??? 现在,关于您的目录结构,首先要问您的MajorClasses文件夹是否位于application / ???中? if so ok, in the same file, as above, there should be a set_include_path() function being run. 如果可以,则在与上述相同的文件中,应该正在运行set_include_path()函数。 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. 在MajorClasses文件夹中,您现在必须为每个命名空间创建目录。 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. 因此,如果您具有名称空间Custom,则可以创建目录,还必须创建Validator目录,因为要像这样命名它,所以路径应该是。

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. Telephone.php应该是您的类文件的名称,类文件名始终是类名中的最后一个名称空间。

Did I miss anything? 我有想念吗?

This question comes under Zend Resource auotloading http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html 此问题来自Zend资源自动加载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 ie class Custom_validator_Telephone , so our namespace for this folder is Custom . 1)假设MajorClasses文件夹下的所有文件都是由Custom启动的,即Custom_validator_Telephone类,因此此文件夹的命名空间为Custom In order to include files under this folder we need to create an instance of zend resource autoloader 为了在此文件夹下包含文件,我们需要创建一个zend资源自动加载器实例

$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. 2)现在我们已经准备好资源自动加载器,我们需要向该对象添加资源,例如,如果我在MajorClasses文件夹中有名称验证器的文件夹,并且该文件夹中的所有文件都以Custom_Validator开头,则此文件夹的命名空间为Validator,因为已经将“自定义”定义为父资源对象的前缀。

$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/. 第二个参数定义相对于实例化资源自动加载器对象时声明的基本路径的文件夹路径,因此该资源的路径为/ path / to / MajorClasses / validators /。
  • 3rd parameter specifies namespace of the class ie 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 第三个参数指定类的名称空间,即它将由资源对象的名称空间(在我们的情况下为Custom )串联,因此, 到目前为止 ,完整类的前缀为Custom_Validator,并且剥离后此文件夹内的php文件将使用该类名称后缀.php文件扩展

3) Now we can put Telephone.php inside validators folder and if we place above code in bootstrap's any function for eg _initPlaceHolders then we can create instance of Custom_Validator_Telephone anywhere in our application without need of using require_once statement. 3)现在我们可以将Telephone.php放在验证器文件夹中,如果将上面的代码放在bootstrap的任何函数中,例如_initPlaceHolders,那么我们可以在应用程序中的任何位置创建Custom_Validator_Telephone的实例,而无需使用require_once语句。

$telValidator = new Custom_Validator_Telephone();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM