Im trying to perform an validation on an array but not getting the desired results. Here is my modal.
i want the numberCount function to validate if there are 5 array elements in my number attribute if not generate an error. Looks like its serialized before its validated. What can i do?
<?php /** * This is the model class for table "userGameNumber". * * The followings are the available columns in table 'userGameNumber': * @property integer $id * @property integer $userId * @property integer $gameId * @property string $number * @property integer $megaball */ class UserGameNumber extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return UserGameNumber the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'userGameNumber'; } /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('number, megaball', 'required'), array('number', 'numberCount'), array('userId, gameId, megaball', 'numerical', 'integerOnly'=>true), array('', 'length', 'max'=>255), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, userId, gameId, number, megaball', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'player'=>array(self::BELONGS_TO, 'User', 'userId'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'id' => 'ID', 'userId' => 'User', 'gameId' => 'Game', 'number' => 'Number', 'megaball' => 'Megaball', ); } /** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. */ public function search() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria=new CDbCriteria; $criteria->compare('id',$this->id); $criteria->compare('userId',$this->userId); $criteria->compare('gameId',$this->gameId); $criteria->compare('number',$this->number,true); $criteria->compare('megaball',$this->megaball); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); } //serialize the data before save to database public function beforeSave() { $this->number= serialize($this->number); return parent::beforeSave(); } //transform the serialized data back into its normal form after find public function afterFind() { $this->number= unserialize($this->number); return parent::afterFind(); } public function numberCount($attribute) { if (count($attribute) != 5) { $this->addError($attribute, 'you did not select enough numbers!'); } } }
i want the numberCount function to validate if there are 5 array elements in my number attribute if not generate an error. Looks like its serialized before its validated. What can i do?