validator

I want to validate the phone number,

use the following code in rules

array('phone','match','/^[ + ]{1}[1-9]\d{0,3}[.]{1}[\d\s]+$/'),

error comes up as following:

CException

Description

Property "CRegularExp​ressionValidator.0" is not defined.

Source File

E:\eclipse\workspace\PDTproject\framework\validators\CValidator.php(126)

00114:            }

00115:            $validator->params=$params;

00116:        }

00117:        else

00118:        {

00119:            $params['attributes']=$attributes;

00120:            if(isset($builtInValidators[$name]))

00121:                $className=Yii::import($builtInValidators[$name],true);

00122:            else

00123:                $className=Yii::import($name,true);

00124:            $validator=new $className;

00125:            foreach($params as $name=>$value)

00126: $validator->$name=$value;

00127:        }

00128:        return $validator;

00129:    }

00130:

00131:    /**

00132:      * Validates the specified object.

00133:      * @param array the list of attributes to be validated. Defaults to null,

00134:      * meaning every attribute listed in {@link attributes} will be validated.

00135:      * @param CModel the data object being validated

00136:      */

00137:    public function validate($object,$attributes=null)

00138:    {

You should specify the pattern with array key 'pattern'.

Not sure if this will help, but I started writing an extension for region-specific phone validation. At this point I only have very basic validation for north-american phone numbers. You may simply tweak the regexp to suit your needs. Eventually I'll add in more exact validation for north-american phone numbers, as well as checking for other regions, but here's what I have atm:



<?php


/**


 * EPhoneValidator validates that a phone number (of specified region) is in a valid format


 *


 *


 * @author luoshiben


 * @package application.extensions.ephonevalidator


 * @since 1.0


  */


class EPhoneValidator extends CValidator


{


    public $region          = 'northamerica';


	public $required		= false;			//if phone is blank, should an error be thrown?


    private $_phoneNumber   = null;


    


	/**


	 * Validates the attribute of the object.


	 * If there is any error, the error message is added to the object.


	 * @param CModel the object being validated


	 * @param string the attribute being validated


	 */


	protected function validateAttribute($object,$attribute)


	{


        $valid = true;


        if(is_object($object) && isset($object->$attribute))


            $this->_phoneNumber = $object->$attribute;


            


        if(isset($this->_phoneNumber) && strcmp($this->_phoneNumber, '')) {


            $validationRegionMethod = $this->region.'PhoneValidator';


            if(method_exists($this,$validationRegionMethod))


                $valid = $this->$validationRegionMethod();


            else


                $valid = $this->genericPhoneValidator();


        }


        else


        {


        	if($this->required)


    			$valid = false;


    	}





		if (!$valid) {


            $message = $this->message !== null ? $this->message : Yii::t('yii', $object->getAttributeLabel($attribute).' is invalid.');


			$this->addError($object, $attribute, $message);


		}


	}


	


	/**


	 * validates a non-region specific phone number based on the non-existence of alpha chars


	 *


	 * @return boolean


	 */


	protected function genericPhoneValidator()


	{


        if (preg_replace('/[^a-z]/i', '', $this->_phoneNumber)) return false;


        else return true;


    }


    


    /**


     * provides basic validation of a north-american phone number based on the non-existence of alpha chars


     * and the existence of 10 or 11 digits


     *


     * @return unknown


     */


    protected function northamericaPhoneValidator()


	{


        $valid = true;


        if (preg_replace('/[^a-z]/i', '', $this->_phoneNumber))


        {


            $valid = false;


        }


        else


        {


            $pn = ereg_replace("[^0-9]",'', $this->_phoneNumber);


            if (strlen($pn) <> 10 || strlen($pn) <> 11)


            {


                $valid = false;


            }


        }


        return $valid;


    }


}


Just create a file called EPhoneValidator.php in your extensions dir, then use it on an attribute(s) in your rules function as 'application.extensions.ephonevalidator'. Hope that helps!

thnak u qiang and luoshiben

array('phone','match','pattern'=>'/^[+]{1}[1-9]\d{0,3}[.]{1}[\d\s]+$/')

it’s done after add ‘pattern’ ;D

Many things I have to learn.