[英]Custom phone validator not working symfony
我正在使用symfony 1.4原则。 我有一个可以验证电话号码的代码,这是代码:(基于此网站“ http://jasonswett.net/blog/how-to-validate-and-sanitize-a-phone-number-in-symfony/” )
我将其粘贴到lib文件夹中。
apps/frontend/lib/myValidatorPhone.class.php
<?php
/**
* myValidatorPhone validates a phone number.
*
* @author Jason Swett (http://jasonswett.net/how-to-validate-and-sanitize-a-phone-number-in-symfony/)
*/
class myValidatorPhone extends sfValidatorBase
{
protected function doClean($value)
{
$clean = (string) $value;
$phone_number_pattern = '/^(\((\d{3})\)|(\d{3}))\s*[-\.]?\s*(\d{3})\s*[-\.]?\s*(\d{4})$/';
if (!$clean && $this->options['required'])
{
throw new sfValidatorError($this, 'required');
}
// If the value isn't a phone number, throw an error.
if (!preg_match($phone_number_pattern, $clean))
{
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
// Take out anything that's not a number.
$clean = preg_replace('/[^0-9]/', '', $clean);
// Split the phone number into its three parts.
$first_part = substr($clean, 0, 3);
$second_part = substr($clean, 3, 3);
$third_part = substr($clean, 6, 4);
// Format the phone number.
$clean = '('.$first_part.') '.$second_part.'-'.$third_part;
return $clean;
}
}
然后我在表单文件夹中创建了此代码
lib/form/doctrine/reservation/reservationApplicationForm.class.php
$this->validatorSchema['telephone'] = new myValidatorPhone(array(
'required' => false,
));
但是当我运行程序时,我得到了错误:
致命错误:在第36行的C:\\ www \\ project \\ reservation \\ lib \\ form \\ doctrine \\ reservationApplicationForm.class.php中找不到类'myValidatorPhone'
基于网站,此代码可在1.4上使用,我现在正在使用它。 但是我不知道为什么会出现致命错误。
在您的lib文件夹中创建文件夹验证器,并将您的验证器放入lib / validator / myValidatorPhone.class.php中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.