I have the validation rules in my model already and they are working fine. BUT in the controller I would like to access the ‘max’ property that tells the max field length so I can do some hacky magic to a text field before I save it and ensure that I do not exceed the set limit which would cause a validation error. I know I can access the model’s field names by looking at the attributes but not sure their is a specific property for an attributes validation rules.
About the only way I can see doing it is to look at the return array of the active records rules() method. This looks to just return an array of the validation array’s with no specific way to look up a rule by field name (if that makes any sense).
What is your goal? If you want to change max value dynamically, you can add new property to the model class:
// in the model's class (Post for example)
class Post extends CActiveRecord
public $max = 255;
public function rules()
array('title', 'length', 'max'=>$this->max),
// other validation rules
// other stuffs
// in the controller's class (PostController for example)
class PostController extends CController
public function actionCreate()
$model = new Post();
$model->max = 10;
$model->title = 'just new article';
$model->body = 'some text...';
// if success
// if there are wrong data
// other stuffs
Nothing quite so complicated, but want to append data to the models text field and ensure I do not go over the max field size or the data will not validate when I call save(). The idea is to pull the max from the database text_field size or the validation rule setting and use that number to ensure that I don’t append more then the field size.
I gave this a try, but it doesn’t work. I don’t think the object has that information. I think the field is just an attribute name that is used for looking up the rest of the meta data somewhere. I’m sure if I dig around in the codebase I can find where to get the meta data for the field, for now going to just hard code the size
Thanks for the suggestions, I’m going to revisit this as I would like to know if I can find out the field information just for the fun of it.
It does not cause an error, but does not return anything for the value of the field. I’ll poke around some more and see if I can see what behind it. I used the call just after creating an instance of the model by ‘new’. And nothing yet done. It may not have the needed data yet until something is actually done with the db, but not sure all speculation