简体   繁体   中英

Custom phone validator not working symfony

I'm using symfony 1.4 doctrine. I have a code that can validate phone numbers here's the code:(Based on this website "http://jasonswett.net/blog/how-to-validate-and-sanitize-a-phone-number-in-symfony/")

I pasted it on lib folder.

 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;
  }
}

Then I created this code on my form folder

 lib/form/doctrine/reservation/reservationApplicationForm.class.php
$this->validatorSchema['telephone'] = new myValidatorPhone(array(
      'required' => false,
    ));

but when I run the program I get the error:

Fatal error: Class 'myValidatorPhone' not found in C:\\www\\project\\reservation\\lib\\form\\doctrine\\reservationApplicationForm.class.php on line 36

Based on the website this code works on 1.4 which I'm using it right now. But I don't know why I got a Fatal error.

在您的lib文件夹中创建文件夹验证器,并将您的验证器放入lib / validator / myValidatorPhone.class.php中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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