Adding to a model's method

Hi

I'm trying to add an extra validation rule to the the 'Lists' model so I can use the extra field I'm adding in the database for my extension.

Can someone tell me the correct way to go about this? I've tried extending the Lists model and creating the $rules method like below:

Code:
class MyCrazyExtCommon extends Lists
{
    public function rules()
    {
        $rules = array(
            array('mycrazy_field', 'in', 'range' => array_keys($this->getYesNoOptions())),
        );
        return CMap::mergeArray($rules, parent::rules());
    }
}
$list->rules() just displays the original array from the Lists model :(

Would really appreciate a little help here - I may be going about this the wrong way!

Cheers

Elliot
 
Last edited:
I'd neglected to call my extended model with:
Code:
Yii::import('ext-my-crazy-extension.common.models.*');
and then use it:
Code:
$mycrazymodel = new MyCrazyExtensionExtCommon;
Only trouble is, the field name and id produced by
Code:
$form->dropDownList($mycrazymodel,'mycrazy_field',$mycrazymodel->getYesNoOptions(),$mycrazymodel->getHtmlOptions('mycrazy_field'));
has "MyCrazyExtensionExtCommon" and not "Lists". So the database is not being updated. I could echo out the select box statically and it would be ok but it doesn't feel semantic.

:(
 

twisted1919

Administrator
Staff member
There's a hook for that, of course:
PHP:
Yii::app()->hooks->addFilter('customer_model_lists_rules', function(CList $rulesList){
    $rulesList->add(array('mycrazy_field', 'length', 'max' => 100));
    return $rulesList;
});
:D
 
Ah cool! Just what I'm looking for :D

What version did that get added - I haven't updated to 1.4.9 yet - I'm on 1.4.3... Will update now...
 
I've added my rule like so:

Code:
// Add the rule to the model
Yii::app()->hooks->addFilter('customer_model_lists_rules', function(CList $rulesList){
    $rulesList->add(array('mycrazy_field', 'in', 'range' => array_keys($list->getYesNoOptions())));
    return $rulesList;
});
The dropdown list looks correct and I've ditched extending the model etc.. but the database does not seem to update. Form shows as being successful but the drop down doesn't update on page reload. :(
 
Top