php - Yii Custom Validation - Class Validate contains 1 abstract method -


i have validation methods need use multiple models. example validation of phone numbers can shared across multiple models.

i understand http://www.yiiframework.com/wiki/56/ can create extension can used multiple models, example:

array('phone', 'ext.validate.validate'), 

i have modified line few times , can confirm hitting right file.

with following in validate.php

class validate 

i error call undefined method validate::applyto(), therefore have changed

class validate extends cvalidator 

as suggested link above, error:

class validate contains 1 abstract method 

here file stands:

<?php  class validate extends cvalidator {     public function phone($phone)     {         if(!ctype_digit($phone))         {             $this->adderror($phone, yii::t('flash','flash.not_authorised',array('{attribute}'=>$phone)).' '.ucfirst(str_replace('_', ' ', $phone)).' field');         }         else         {             return true;         }     } } 

can point me in right direction how can have shared validation between multiple methods using above.

ok managed solve this.

i found following after few hours of searching http://www.yiiframework.com/wiki/56/

the main issue tat need following method in class, when extending cvalidator:

protected function validateattribute($object,$attribute) {  } 

here model code (yii/protected/extension/validate/validate.php)

array('phone', 'ext.validate.validate'), 

here class code:

<?php  class validate extends cvalidator {     protected function validateattribute($object,$attribute)     {         self::{$attribute."validation"}($object,$attribute);        }       protected function phonevalidation($object,$attribute)     {         if(!empty($object->$attribute))         {             if(!ctype_digit($object->$attribute))             {                  $this->adderror($object,$attribute,yii::t('app','validation.telephon_failed',array('{attribute}'=>ucwords($attribute))));             }         }     } } 

Comments